Puchong
Puchong

Reputation: 41

How to used "web request by spring mvc" and "rest by jersey" in spring boot

Hi i want to handle web request by spring mvc and handle rest by jersey in the same project (Spring-Boot)

As i test Rest service is working but web is not How can i set Application Config ?

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = {ProductsResource.class, MessageService.class,Web.class})
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
        return registration;
    }


    @Bean
    public ServletRegistrationBean webMVC() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();

        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(ResourceConfig.class);
        dispatcherServlet.setApplicationContext(applicationContext);

        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "*.html");
        servletRegistrationBean.setName("web-mvc");
        return servletRegistrationBean;
    }

Web Controller

@Controller
@Component
public class Web {

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

    @RequestMapping("/bar")
    String bar() {
        return "bar";
    }

}

Rest Controller

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

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/hello")
    public String hello() {
        return "Hello World";
    }
}

Upvotes: 4

Views: 1553

Answers (1)

Hasasn
Hasasn

Reputation: 908

Actually with spring boot (I'm talking about version 1.4.x), it's pretty easy to do Spring MVC and JAX-RS(rest by jersey) at the same time :). You don't need any servlet registrations. All you need to do is to add a Configuration class like the following

@Configuration
@ApplicationPath("/rest")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        packages("your.package.with.rest.resources");
    }
}

Now all your JAXRS resources are served under /rest/*

For example, your Rest Controller can be refactored as

@Path("/hello")
@Component
public class ProductsResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String hello() {
        return "Hello World";
    }
}

Now if you hit url http://server:port/rest/hello, Hello World should be returned.

Finally, remember to add the following dependencies in your maven pom file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

That should work for you.

Upvotes: 1

Related Questions