简体   繁体   English

从JaxRS迁移到Spring Rest

[英]Migration from JaxRS to Spring Rest

I am trying to migrate from Jax RS to Spring Rest, I am not sure how to add base address of URL for Spring rest. 我正在尝试从Jax RS迁移到Spring Rest,我不确定如何为Spring rest添加URL的基地址。

I tried adding <mvc:annotation-driven /> , but did not help 我尝试添加<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>

Here is my 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
}

How to add Spring rest instead of jaxrs:server in beans.xml. 如何在beans.xml中添加Spring rest而不是jaxrs:server。

For spring to discover the annotated class, you need to add the annotation-driven tag and also component-scan : 为了让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>

The alternative is to use AnnotationConfigApplicationContext when you load the spring context, which is a fully java / annotation configured setup instead of the XML config. 替代方法是在加载spring上下文时使用AnnotationConfigApplicationContext ,这是完全由Java /注释配置的设置,而不是XML配置。

For the gory details, refer to the spring docs https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-metadata : 有关详细信息,请参阅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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM