2016-04-28 99 views
1

我想使用Spring + CXF創建一個RESTfull web服務,無需配置XML bean。我如何將以下基於XML的配置轉換爲Java?沒有xml的Spring CXF配置

applicationBean.xml

<jaxrs:server id="employeeService" address="/employeeservices"> 
    <jaxrs:providers> 
     <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" /> 
    </jaxrs:providers> 
    <jaxrs:serviceBeans> 
     <ref bean="cxfServiceImpl" /> 
    </jaxrs:serviceBeans> 
    <jaxrs:extensionMappings> 
     <entry key="xml" value="application/xml" /> 
     <entry key="json" value="application/json" /> 
    </jaxrs:extensionMappings> 
</jaxrs:server> 

春CXF配置

@Configuration 
public class AppConfig { 
    //Code for CXF need to be here 
} 
+0

檢查這鏈接 - http://stackoverflow.com/questions/36614351/how-to-expose-a-cxf-restful-web-service-in-java/36615090#36615090 – Sampada

回答

1

,如果你使用的是彈簧啓動,這裏是最簡單的方法。

@Import(SpringComponentScanServer.class) 
@Configuration 
public class KpCxfConfiguration { 

    @Bean 
    public ServletRegistrationBean servletRegistrationBean(ApplicationContext context) { 
     return new ServletRegistrationBean(new CXFServlet(), "/api/*"); 
    } 

} 

注:您的服務類需要與@Path的註解

如果您使用非彈簧啓動應用程序,你可以在web.xml中定義的servlet映射