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 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!