一、创建动态web工程,并导入jar包到lib目录下,add build bath
二、创建bean(Person .java),用于接受和返回数据
package com.dgut.bean;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "person")
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三、创建实现类(personServiceImpl .java)
package com.dgut.personService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import com.dgut.bean.Person;
@Path(value = "/person/{id}")
public class personServiceImpl {
@GET
@Path(value = "getPersonById")
@Produces("application/json")
public Person getPersonById(@PathParam("id") int id, @QueryParam("name") String name) {
Person person = new Person();
person.setId(id);
person.setName("王二毛");
return person;
}
@GET
@Path(value = "getPersonByName")
@Produces("application/xml")
public Person getPersonByName(@PathParam("id") int id, @QueryParam("name") String name) {
Person person = new Person();
person.setId(id);
person.setName("王二毛");
return person;
}
}
四、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>CxfSpringWeb</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
五、配置cxf-servlet.xml(注意:这里使用的时jaxrs,而不是jaxws)
<?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:soap="http://cxf.apache.org/bindings/soap"
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/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"
>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<bean id="personServiceId" class="com.dgut.personService.personServiceImpl"></bean>
<jaxrs:server id="personServiceBean" address="/personService">
<jaxrs:serviceBeans>
<ref bean="personServiceId"/>
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
六、运行index.jsp文件,通过地址栏进行访问,以验证
6.1、json风格
6.2、xml风格
七、客户端接收并解析json数据
7.1、创建Java工程,并导入需要的jar包
7.2、创建Java测试文件(test01.java)
package com.dgut.test01;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import net.sf.json.JSONObject;
public class test01 {
public static void main(String[] args) throws Exception {
String urlStr = "http://localhost:8080/CxfSpringWeb/ws/personService/person/1/getPersonById";
URL url = new URL(urlStr);
StringBuffer document = new StringBuffer();
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line);
}
reader.close();
System.out.println(document);
JSONObject json = JSONObject.fromObject(document.toString());
System.out.println(json.toString());
JSONObject obj = (JSONObject) json.get("person");
System.out.println(obj.toString());
String val = obj.getString("name");
System.out.println(val);
}
}
7.3、运行test01.java文件进行测试