问题描述
我的应用程序基于spring-boot和jersey.我已经在我的应用程序中配置了Togglz.我可以成功启动Web应用程序,但运行gradle bootRun.从启动期间的输出中,我可以看到以下日志消息.但是我无法访问 http://localhost:8080/togglz .我的应用程序根目录路径是"/",由jersey管理.看来spring-mvc在togglz上可以很好地工作,但是在与jersey集成时无法访问它.我该怎么做才能让球衣接受网址?
My application based on spring-boot and jersey. I have configured togglz in my application. I can successfully launch my web application but running gradle bootRun. From the output during the startup, I am able to see below log message. But I am not able to access http://localhost:8080/togglz. My application root path is "/" which is managed by jersey. It seems that spring-mvc works well with togglz but failed to access it when integrating with jersey. What should I do in order to let jersey to accept the url?
2016-03-30 18:40:35.191 INFO 81748 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/togglz || /togglz.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
推荐答案
为使Jersey 2与Spring Boot应用程序中的Spring MVC端点一起使用,我建议确保您的application.yml (或.properties进行此类区分,例如:
For Jersey 2 to work along with Spring MVC endpoints in a Spring Boot application, I would suggest to make sure your application.yml (or .properties makes such distinction, something like:
...
# 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-using-jersey
You could read more about this at my blog: http://tech.asimio.net/2016/04/05/Microservices-using-Spring-Boot-Jersey-Swagger-and-Docker.html#implement-api-endpoints-using-jersey
如果您要在Spring Boot应用程序中将Jersey 1与Spring MVC端点集成,Spring Boot不会提供jersey 1启动程序,因此所有内容都需要手动"配置,但基本上是Jersey servlet映射到"/",您需要对其进行配置,以将404传递给servlet容器以进行进一步处理(可能是Spring MVC或普通servlet端点).像这样:
If you are integrating Jersey 1 with Spring MVC endpoints in a Spring Boot app, Spring Boot doesn't provide a jersey 1 starter so everything needs to be "manually" configured, but basically if your Jersey servlet is mapped to "/", you would need to configure it to let pass 404 to the servlet container for further handling (maybe a Spring MVC or plain servlet endpoint). Something like:
Jersey 1资源配置:
Jersey 1 resources configuration:
@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());
}
...
}
泽西岛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
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("/*");
}
};
}
application.yml
application.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和使用Spring-Cloud-Eureka-Ribbon-and-Feign.html#create-the-demo-service-1
More about Jersey 1 and Spring Boot / Cloud could be found at my blog: http://tech.asimio.net/2016/11/14/Microservices-Registration-and-Discovery-using-Spring-Cloud-Eureka-Ribbon-and-Feign.html#create-the-demo-service-1
这篇关于与spring-boot和jersey集成时如何查找togglz网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!