Spring Boot 中的 JAX-RS 和 MVC [英] JAX-RS and MVC at Spring Boot
问题描述
我有一个用作 MVC 的 Spring Boot 应用程序.我想在我的应用程序中使用 JAX-RS 而不使用 Spring 注释.我将在不同的类中同时使用 JAX-RS 注释组件和 MVC 组件.当我添加 Jersey 资源配置时(不注册任何端点):
I have a Spring Boot application which works as MVC. I would like to use JAX-RS at my application without using Spring annotations. I'll have both JAX-RS annotated components and MVC components at different classes. When I add a Jersey Resource Config (without registering any endpoint):
@Component
public class JerseyConfig extends ResourceConfig {
}
我启动了应用程序,但没有显示登录页面.当我打开登录页面时,它像文档一样被下载.我该如何解决?
I startup the application and login page is not shown. Its been downloaded as like a document when I open login page. How can I solve it?
推荐答案
1) 确保您的应用程序的 Spring Boot 配置文件区分 Spring MVC(例如执行器端点)和 Jersey(资源端点):
1) Make sure your app's Spring Boot configuration file makes a distinction between Spring MVC, for actuator endpoints for instance and Jersey for resources endpoints:
application.yml
...
# 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
...
2)确保您的 Spring Boot 应用程序通过以下方式扫描位于特定包(即 com.asimio.jerseyexample.config)中的组件:
2) Make sure your Spring Boot app scans for components located in specific packages (ie com.asimio.jerseyexample.config) via:
@SpringBootApplication(
scanBasePackages = {
"com.asimio.jerseyexample.config", "com.asimio.jerseyexample.rest"
}
)
3) Jersey 配置类实现:
3) Jersey configuration class implementation:
package com.asimio.jerseyexample.config;
...
@Component
public class JerseyConfig extends ResourceConfig {
...
public JerseyConfig() {
// Register endpoints, providers, ...
this.registerEndpoints();
}
private void registerEndpoints() {
this.register(HelloResource.class);
// Access through /<Jersey's servlet path>/application.wadl
this.register(WadlResource.class);
}
}
4) 使用 JAX-RS (Jersey) 的资源实现:
4) Resource implementation using JAX-RS (Jersey):
package com.asimio.jerseyexample.rest.v1;
...
@Component
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class HelloResource {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloResource.class);
@GET
@Path("v1/hello/{name}")
public Response getHelloVersionInUrl(@ApiParam @PathParam("name") String name) {
LOGGER.info("getHelloVersionInUrl() v1");
return this.getHello(name, "Version 1 - passed in URL");
}
...
}
可以在我几个月前创建的博客中找到更详细的操作方法,使用 Spring Boot、Jersey Swagger 和 Docker 的微服务
A more detailed how-to could be found at a blog I created a few months ago, Microservices using Spring Boot, Jersey Swagger and Docker
这篇关于Spring Boot 中的 JAX-RS 和 MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!