根據請求頭accept的內容(application/xml、application/json)動態的讓服務端響應xml或json數據(同理也能夠經過content-type動態的進行xml或json參數請求)java
1. 定義webservice服務端與實體類web
@Service("cxfService") public class TestCxfJsonXml { @POST @Path(value = "/person") @Produces({ MediaType.APPLICATION_JSON ,MediaType.APPLICATION_XML}) @Consumes({ MediaType.APPLICATION_JSON }) public Person info(Person person){ System.out.println(JSON.toJSONString(person,true)); return person; } @Data //lombok @XmlRootElement(name="person") static class Person{ private String name; private int age; } }
2. 經過配置將服務註冊到cxfspring
<?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:context="http://www.springframework.org/schema/context" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <!-- 初始化cxf servlet --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- 日誌攔截器bean,可自定義攔截器 --> <bean id="loggingInInterceptor" class="org.apache.cxf.ext.logging.LoggingInInterceptor" /> <bean id="loggingOutInterceptor" class="org.apache.cxf.ext.logging.LoggingOutInterceptor" /> <!-- 發佈方式1:使用JAXRSServerFactoryBean --> <jaxrs:server address="/service"> <jaxrs:serviceBeans> <ref bean="cxfService" /> </jaxrs:serviceBeans> <!-- Provider 進行json、xml解析的實現類--> <jaxrs:providers> <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" /> <bean class="com.fasterxml.jackson.jaxrs.xml.JacksonXMLProvider" /> </jaxrs:providers> <!-- 輸入日誌攔截器 --> <jaxrs:inInterceptors> <ref bean="loggingInInterceptor" /> </jaxrs:inInterceptors> <!-- 輸出日誌攔截器 --> <jaxrs:outInterceptors> <ref bean="loggingOutInterceptor" /> </jaxrs:outInterceptors> </jaxrs:server> </beans>
3. pom文件引入json、xml解析實現類(其餘引入省略)apache
<dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.4.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-xml-provider</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-base</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.9</version> </dependency>
4. 經過HTTPClient進行json請求,json響應json
HttpClient client = new HttpClient(); PostMethod post = new PostMethod(url); RequestEntity entity = null; client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8"); entity = new StringRequestEntity("{\"name\":\"tom\",\"age\":28}", "application/json", "UTF-8"); post.setRequestEntity(entity); post.setRequestHeader("Accept","application/json"); client.executeMethod(post); String response = post.getResponseBodyAsString();
5. 經過HTTPClient進行json請求,xml響應app
HttpClient client = new HttpClient(); PostMethod post = new PostMethod(url); RequestEntity entity = null; client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8"); entity = new StringRequestEntity("{\"name\":\"tom\",\"age\":28}", "application/json", "UTF-8"); post.setRequestEntity(entity); post.setRequestHeader("Accept","application/xml"); client.executeMethod(post); String response = post.getResponseBodyAsString();