cxf集成到spring中發布restful webservice
一、maven依賴
<!--cxf--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.1</version> </dependency>
二、web.xml配置
<servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/cxf/*</url-pattern> </servlet-mapping>
三、spring配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" default-lazy-init="true"> <description>Apache CXF resutful Web Service配置</description> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="webservice" class="WebserviceImpl"/> <jaxrs:server id="cxfService" address="/ws"> <jaxrs:serviceBeans> <ref bean="webservice" /> </jaxrs:serviceBeans> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:languageMappings> <entry key="en" value="en-gb"/> </jaxrs:languageMappings> <jaxrs:providers> <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/> </jaxrs:providers> </jaxrs:server> </beans>
四、接口類(實現類省略了):
package webservice;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/test")
public interface Webservice {
@GET
@Path("/getText")
@Produces({ MediaType.TEXT_PLAIN })
String getText(@QueryParam("param1")String param1, @QueryParam("param2")String param2);
@POST
@Path("/postText")
@Produces({ MediaType.TEXT_PLAIN })
String postText(@FormParam("param1")String param1, @FormParam("param2")String param2);
@GET
@Path("/getJson")
@Produces({ MediaType.APPLICATION_JSON })
Map<String, Object> getJson(@QueryParam("param1")String param1, @QueryParam("param2")String param2);
@POST
@Path("/postJson")
@Produces({ MediaType.APPLICATION_JSON })
Map<String, Object> postJson(@FormParam("param1")String param1, @FormParam("param2")String param2);
}
五、測試代碼:
/**
* 獲取JAXRS WebService的結果信息
*/
public static String postJAXRSWebService(String processURL){
System.out.println( processURL);
String result = "";
try {
//創建一個HttpClient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
//創建HttpPost對象
HttpPost postRequest =new HttpPost(processURL);
List <NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1","144203923"));
params.add(new BasicNameValuePair("param2","中國"));
//發出HTTP request
UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity(params, "UTF-8");
postRequest.setEntity(paramEntity);
System.out.println("------------------------------------------------------");
System.out.println(paramEntity.getContentType());
System.out.println(paramEntity.getContentLength());
System.out.println(EntityUtils.getContentCharSet(paramEntity));
System.out.println(EntityUtils.toString(paramEntity));
System.out.println("--------------------------------------------------");
CloseableHttpResponse response =httpclient.execute(postRequest);
//獲取HttpEntity
HttpEntity entity=response.getEntity();
//獲取響應的結果信息
result =EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 獲取JAXRS WebService的結果信息
*/
public static String getJAXRSWebService(String processURL){
System.out.println( processURL);
String result = "";
try {
//創建一個HttpClient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
//創建HttpGet對象
HttpGet request=new HttpGet(processURL);
CloseableHttpResponse response =httpclient.execute(request);
//獲取HttpEntity
HttpEntity entity=response.getEntity();
//獲取響應的結果信息
result =EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
} 本文由用戶 bmbm 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!