[英]Migration from JaxRS to Spring Rest
我正在嘗試從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>
這是我的控制器:
@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 docs https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-metadata :
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.