我正在尝试从 Jax RS 迁移到 Spring Rest,我不知道如何为 Spring Rest 添加 URL 的基地址。
我尝试添加 <mvc:annotation-driven />
,但没有帮助
<jaxrs:server address="/search-service">
<jaxrs:serviceBeans>
...all service classes
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="wadlGenerator"/>
<ref bean="cors-filter"/>
<ref bean="soaJsonJaxbProvider"/>
<ref bean="exceptionMapper"/>
<ref bean="searchContextProvider"/>
</jaxrs:providers>
</jaxrs:server>
这是我的 Controller :
@RestController
@RequestMapping(value = "/search", produces=org.springframework.http.MediaType.APPLICATION_JSON_VALUE,
consumes=org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
public class SearchService {
@PostMapping
public ServiceResponse<searchDTO> search(@RequestHeader HttpHeaders headers, ServiceRequest<searchDTO> request) {
....method
}
如何在 beans.xml 中添加 Spring Rest 而不是 jaxrs:server。
最佳答案
为了让 spring 发现带注解的类,您需要添加 annotation-driven
标签以及 component-scan
:
<?xml version="1.0" encoding="UTF-8"?>
<beans> <!-- add in all the namespace declarations you need-->
<context:component-scan base-package="com.yourpackage" />
<mvc:annotation-driven />
</beans>
另一种方法是在加载 spring 上下文时使用 AnnotationConfigApplicationContext,这是一个完全 java/注释配置的设置,而不是 XML 配置。
有关详细信息,请参阅 spring 文档 https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-metadata :
关于java - 从 JaxRS 迁移到 Spring Rest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57683674/