Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest “如何使用”;“spring mvc的web请求”;及;由泽西岛休息”;春靴_Rest_Spring Mvc_Jersey_Spring Boot - Fatal编程技术网

Rest “如何使用”;“spring mvc的web请求”;及;由泽西岛休息”;春靴

Rest “如何使用”;“spring mvc的web请求”;及;由泽西岛休息”;春靴,rest,spring-mvc,jersey,spring-boot,Rest,Spring Mvc,Jersey,Spring Boot,嗨,我想通过SpringMVC处理web请求,通过jersey处理rest 在同一项目中(Spring Boot) 当我测试Rest服务是否正常工作时,web却不正常 如何设置应用程序配置 @SpringBootApplication @EnableAutoConfiguration @ComponentScan(basePackageClasses = {ProductsResource.class, MessageService.class,Web.class}) public class

嗨,我想通过SpringMVC处理web请求,通过jersey处理rest 在同一项目中(Spring Boot)

当我测试Rest服务是否正常工作时,web却不正常 如何设置应用程序配置

@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;
    }
网络控制器

@Controller
@Component
public class Web {

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

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

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

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

@Controller
@Component
public class Web {

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

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

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

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/hello")
    public String hello() {
        return "Hello World";
    }
}
实际上,使用SpringBoot(我说的是1.4.x版),同时使用SpringMVC和JAX-RS(jersey提供的rest)非常容易:)。您不需要任何servlet注册。您只需添加一个
配置
类,如下所示

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

    public JerseyConfig() {
        packages("your.package.with.rest.resources");
    }
}
现在,您的所有JAXRS资源都在
/rest/*

例如,您的Rest控制器可以重构为

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

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String hello() {
        return "Hello World";
    }
}
现在如果你点击url
http://server:port/rest/hello
应返回Hello World

最后,记住在maven pom文件中添加以下依赖项

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

org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
春靴起跑服
那应该对你有用