为了让 Jersey 2 与 Spring Boot application 中的 Spring MVC 端点一起工作,我建议确保您的 application.yml (或 .properties 做出这样的区分,例如:
...
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...)
server.servlet-path: /
# Jersey dispatcher servlet
spring.jersey.application-path: /api
...
您可以在我的博客上阅读更多相关信息:http: //tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger-and-Docker.html#implement-api-endpoints-使用球衣
如果您在 Spring Boot 应用程序中将 Jersey 1 与 Spring MVC 端点集成,则 Spring Boot 不提供 jersey 1 启动器,因此所有内容都需要“手动”配置,但基本上如果您的 Jersey servlet 映射到“/”,您需要对其进行配置以将 404 传递给 servlet 容器以进行进一步处理(可能是 Spring MVC 或普通 servlet 端点)。就像是:
Jersey 1 资源配置:
@ApplicationPath("/")
public class DemoResourcesConfig extends PackagesResourceConfig {
private static final Map<String, Object> properties() {
Map<String, Object> result = new HashMap<>();
result.put(PackagesResourceConfig.PROPERTY_PACKAGES, "com.sun.jersey;com.asimio.api.demo1.rest");
// To forward non-Jersey paths to servlet container for Spring Boot actuator endpoints to work.
result.put("com.sun.jersey.config.feature.FilterForwardOn404", "true");
result.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
return result;
}
public DemoResourcesConfig() {
super(properties());
}
...
}
Jersey 1 资源实现:
package com.asimio.api.demo1.rest;
...
@Component
@Path("/actors")
@Produces(MediaType.APPLICATION_JSON)
public class ActorResource {
@GET
public List<Actor> findActors() {
...
}
@GET
@Path("{id}")
public Actor getActor(@PathParam("id") String id) {
...
}
...
}
ServletContextInitializer bean
@Bean
public ServletContextInitializer servletInitializer() {
return new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
final ServletRegistration.Dynamic appServlet = servletContext.addServlet("jersey-servlet", new SpringServlet());
Map<String, String> filterParameters = new HashMap<>();
// Set filter parameters
filterParameters.put("javax.ws.rs.Application", "com.asimio.api.demo1.config.DemoResourcesConfig");
appServlet.setInitParameters(filterParameters);
appServlet.setLoadOnStartup(2);
appServlet.addMapping("/*");
}
};
}
应用程序.yml
...
# For Spring MVC to enable Endpoints access (/admin/info, /admin/health, ...) along with Jersey
server.servlet-path: /admin
...
更多关于 Jersey 1 和 Spring Boot / Cloud 的信息可以在我的博客中找到:http: //tech.asimio.net/2016/11/14/Microservices-Registration-and-Discovery-using-Spring-Cloud-Eureka-Ribbon- and-Feign.html#create-the-demo-service-1