0

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()
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

1 Answers1

0

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
...

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

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 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());
  }
...
}

Jersey 1 resource implementation:

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
...

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

ootero
  • 3,235
  • 2
  • 16
  • 22