简体   繁体   中英

JerseyConfig overrides @RestController in spring boot application

I am trying to use JerseyConfig class in existing sping boot application but for some reason when I add this configuration:

    @Component
    public class JerseyConfig extends ResourceConfig  {
    public JerseyConfig() {
         register(Users.class);
         register(Groups.class);
         property("SCIM_IMPLEMENTATION_INSTANCE", new JerseyApplication());
      }
    }

The @RestController endpoints do not work as expexted anymore. All of them return 404 after applying this JerseyConfig class. All of the Jersey endpoints work fine.

Can I use JAX rs endpoints (in my case I use Jersey) and @RestCotroller in the same application? I need some configuration for separating my existing REST servcices from the new JAX rs endpoints. If anybody can help I will really appreciate it. Thank you!

There is a workaround for combining Jersey resources and Spring controllers. There are a couple of changes that you will need to do to your setup.

  1. Change annotation of JerseyConfig from @Component to @Configuration and add package of your controllers to scan

    // scan the resources package for your resources / restControler public JerseyConfig() { // other code packages(package_of_your_rest_controller); }

  2. Change annotation of your rest controller from @ReuestMapping to @Path Eg if your controller is like this:

    @RestController @Component public class MyRestController {

     @RequestMapping("/foo") public String foo() { return "foo"; } 

    }

    It becomes this:

    @Path("/") @Component public class MyRestController {

     @Path("/foo") public String foo() { return "foo"; } 

    }

Give this a try once if you can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM