Recently the company internal system to do data docking, so use jersey to do restful WebService interface design. Since spring Boot has integrated jersey, it is expected to import Spring-boot-starter-jersey directly.
In the test, in addition to encountering Chinese garbled spent a relatively long time, the rest of the temporary did not encounter big problems. However, a pit was found when it was released.
Public classJerseyconfigextendsResourceConfig { PublicJerseyconfig () {//registerclasses (Findallclasses ());Packages ("com.xx"); //Registration LogRegister (loggingfeature.class); //Exception HandlingRegister (Exceptionhandler.class); //cross-domain filter registrationRegister (Corsfilter.class); }}
Jersey is registered or scanned using the Register (class Clazz) or packages ("PackageName") to load the required classes into the container. However, in the actual project, the use of register class Clazz will write a large number of registration classes, the dependency is too strong, and in the MAVEN multi-module more difficult to handle.
While using packages to scan, there is no problem when developing tests, but using spring boot to run as a jar will produce filenotfound errors because the mechanism of packages scanning using jersey cannot be scanned to The class file in the jar.
It is possible to use the Spring boot,spring component scan. Therefore, refer to the spring scanning mechanism of the source code, the jar of the class file loaded in the good.
The code is as follows:
Importlombok.extern.slf4j.Slf4j;Importorg.glassfish.jersey.logging.LoggingFeature;ImportOrg.glassfish.jersey.server.ResourceConfig;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.core.io.Resource;ImportOrg.springframework.core.io.support.PathMatchingResourcePatternResolver;ImportOrg.springframework.core.io.support.ResourcePatternResolver;Importorg.springframework.core.type.classreading.CachingMetadataReaderFactory;ImportOrg.springframework.core.type.classreading.MetadataReader;Importorg.springframework.util.ClassUtils;Importjava.io.IOException;ImportJava.util.HashSet;ImportJava.util.Set;/** * @authorGongtao *@version2018-04-23 10:15 **/@Configuration @slf4j Public classJerseyconfigextendsResourceConfig { PublicJerseyconfig () {//Scan Registrationregisterclasses (Findallclasses ()); //Registration LogRegister (loggingfeature.class); //Exception HandlingRegister (Exceptionhandler.class); //cross-domain filter registrationRegister (Corsfilter.class); } /*** Since spring boot is packaged as a jar package, Jersey packages cannot scan files for the folder corresponding to the jar, so the custom package scan *@return */ PrivateSet<class<?>>findallclasses () {String scanpackages= "Com.xxx.eoms.innerinterface.interfaces.resource.*"; ClassLoader Loader= This. GetClass (). getClassLoader (); Resource[] Resources=NewResource[0]; Try{Resources=Scan (loader, scanpackages); } Catch(IOException e) {log.error ("Load Class Exception", E); } returnCONVERT (loader, resources); } /*** Scan jar Package *@paramLoader *@paramPackageName *@return * @throwsIOException*/ PrivateResource[] Scan (ClassLoader loader, String packagename)throwsIOException {resourcepatternresolver resolver=Newpathmatchingresourcepatternresolver (loader); String pattern= "classpath*:" + classutils.convertclassnametoresourcepath (packagename) + "/**/*.class"; returnresolver.getresources (pattern); } /*** Load class *@paramLoader *@paramResource *@return */ PrivateClass<?>loadclass (ClassLoader loader,resource Resource) {Try{cachingmetadatareaderfactory metadatareaderfactory=Newcachingmetadatareaderfactory (loader); Metadatareader Reader=Metadatareaderfactory.getmetadatareader (Resource); returnClassutils.forname (Reader.getclassmetadata (). GetClassName (), loader); } Catch(Linkageerror |classnotfoundexception e) { if(log.isdebugenabled ()) {Log.debug ("Ignoring candidate class resource" + Resource + "due to" +e); } return NULL; } Catch(Throwable e) {if(log.iswarnenabled ()) {Log.warn ("Unexpected failure when loading class resource" +resource, E); } return NULL; } } /*** Resources converted to set<class> *@paramLoader *@paramResources *@return */ PrivateSet<class<?>>convert (ClassLoader loader,resource[] resources) {Set<Class<?>> Classset =NewHashset<>(resources.length); for(Resource resource:resources) {Class<?> Clazz =loadclass (loader, Resource); if(Clazz! =NULL) {classset.add (clazz); } } returnClassset; }
Jersey Add Packages Scan path support in spring boot