如何在与spring-boot和jersey整合时找到togglz url

时间:2016-03-30 10:45:33

标签: spring-boot jersey togglz

我的应用程序基于弹簧靴和平针织物。我在我的应用程序中配置了togglz。我可以成功启动我的Web应用程序,但运行gradle bootRun。从启动期间的输出,我能够看到下面的日志消息。但我无法访问http://localhost:8080/togglz。我的应用程序根路径是“/”,由泽西管理。看起来spring-mvc与togglz配合得很好但是在与球衣整合时无法访问它。我该怎么办才能让球衣接受网址?

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()

1 个答案:

答案 0 :(得分:0)

对于Jersey 2与Spring Boot应用程序中的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-using-jersey

如果您在Spring Boot应用程序中将Jersey 1与Spring MVC端点集成,则Spring Boot无法提供jersey 1启动器,因此一切都需要"手动&#34 ;配置,但基本上如果你的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("/*");
      }
    };
  }

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-and-Discovery-using-Spring-Cloud-Eureka-Ribbon-and-Feign.html#create-the-demo-service-1