Skip to content

Instantly share code, notes, and snippets.

@massahud
Last active November 3, 2017 10:09
Show Gist options
  • Save massahud/c6c6d173ec20f6476f8977ec41a66ab7 to your computer and use it in GitHub Desktop.
Save massahud/c6c6d173ec20f6476f8977ec41a66ab7 to your computer and use it in GitHub Desktop.
Swagger jackson jersey springboot java
import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.Provider;
import io.swagger.converter.ModelConverters;
import io.swagger.jackson.ModelResolver;
import io.swagger.jaxrs.config.BeanConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Service;
@Service
@ApplicationPath("/api")
public class JerseyConfiguration extends ResourceConfig {
@Inject
JerseyConfiguration(ObjectMapper mapper) {
register(org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
//swagger
register(io.swagger.jaxrs.listing.ApiListingResource.class);
register(io.swagger.jaxrs.listing.SwaggerSerializers.class);
ModelConverters.getInstance().addConverter(new ModelResolver(mapper));
BeanConfig beanConfig = new BeanConfig();
}
/** for springboot uber jar */
private void scanPackages(String... packages) {
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Path.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(Provider.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(Consumes.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(Produces.class));
for (String pack : packages) {
registerClasses(
scanner.findCandidateComponents(pack).stream().map(BeanDefinition::getBeanClassName).map(x -> {
try {
return Class.forName(x);
} catch (ClassNotFoundException e) {
return null;
}
}).collect(Collectors.toSet())
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment