配置步骤
开发基于CXF的 RESTful WebService 1.创建Web项目并导入CXF的jar 2.在Web.xml中配置 CXFServlet <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> 3.创建实体类 Person @XmlRootElement(name = "person") public class Person implements Serializable { private String id; private String name; private String description; @XmlElement(name = "id") public String getId() { return id; } public void setId(String id) { this.id = id; } @XmlElement(name = "name") public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement(name = "description") public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } 4.定义 PersonService 接口 //@Produces( { MediaType.APPLICATION_XML }) @Produces( { MediaType.APPLICATION_JSON }) public interface PersonService extends Serializable { @GET @Path("/persons") public List<Person> getPersons(); @GET @Path("/persons/{id}") public Person getPerson(@PathParam("id") String id); } 5.提供实现类 PersonServiceImpl public class PersonServiceImpl implements PersonService { private static final long serialVersionUID = 1L; private static Map<String, Person> ps = new HashMap<String, Person>(); static { Person info = new Person(); p1.setId("1"); p1.setName("zhangsan"); p1.setDescription("hello"); Person p1 = new Person(); p2.setId("2"); p2.setName("lisi"); p2.setDescription("lisi hehe"); ps.put(p1.getId(), p1); ps.put(p2.getId(), p2); } @Override public Person getPerson(String id) { return ps.get(id); } @Override public List<Person> getPersons() { return new ArrayList(ps.values()); } } 6.提供CXF 的配置文件 cxf-servlet.xml <?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:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="personService" class="cn.itcast.cxf.PersonServiceImpl" /> <jaxrs:server id="RestService" address="/v1"> <jaxrs:serviceBeans> <ref bean="personService" /> </jaxrs:serviceBeans> <jaxrs:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxrs:inInterceptors> <jaxrs:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxrs:outInterceptors> </jaxrs:server> </beans> 7.访问CXFServlet:http://localhost/CXF_01/ws 8.通过 URLConnection 访问WebService服务: URLConnection conn = new URL("http://localhost/CXF_01/ws/v1/persons").openConnection(); HttpURLConnection con = (HttpURLConnection) conn; con.setRequestMethod("GET"); int code = con.getResponseCode(); System.out.println(code); InputStream is = con.getInputStream(); int len = 0; byte[] b = new byte[1024]; StringBuffer sb = new StringBuffer(); while((len = is.read(b)) != -1){ String s = new String(b,0,len,"UTF-8"); sb.append(s); } System.out.println(sb.toString()); con.disconnect();
示例配置了
http://i.cnblogs.com/EditPosts.aspx?postid=3806298 之后
配置RESTful services服务,注意 概念和 soap wsdl完全不一样的
但也是开服务 webservice
Person.java
1 package cn.itcast.domain; 2 3 import java.io.Serializable; 4 5 import javax.xml.bind.annotation.XmlElement; 6 import javax.xml.bind.annotation.XmlRootElement;
//通过注解,在网上序列号成xml或者json方式 7 @XmlRootElement(name = "Person") 8 public class Person implements Serializable { 9 private String id; 10 private String name; 11 private String desc; 12 @XmlElement(name="id") 13 public String getId() { 14 return id; 15 } 16 public void setId(String id) { 17 this.id = id; 18 } 19 @XmlElement(name="name") 20 public String getName() { 21 return name; 22 } 23 public void setName(String name) { 24 this.name = name; 25 } 26 @XmlElement(name="desc") 27 public String getDesc() { 28 return desc; 29 } 30 public void setDesc(String desc) { 31 this.desc = desc; 32 } 33 @Override 34 public String toString() { 35 return "Person [desc=" + desc + ", id=" + id + ", name=" + name + "]"; 36 } 37 38 39 }
IPersonService.java
1 package cn.itcast.restful; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 import javax.ws.rs.GET; 7 import javax.ws.rs.Path; 8 import javax.ws.rs.PathParam; 9 import javax.ws.rs.Produces; 10 import javax.ws.rs.core.MediaType; 11 12 import cn.itcast.domain.Person; 13 //@Produces( { MediaType.APPLICATION_XML }) xml格式,以下是json格式 传 14 @Produces( { MediaType.APPLICATION_JSON }) 15 public interface IPersonService extends Serializable { 16 17 @GET 18 @Path(value="/persons/{id}") 19 public Person findPersonById( 20 @PathParam("id") 21 String id); 22 23 @GET 24 @Path(value="/persons") 25 public List<Person> findAllPerson(); 26 }
PersonServiceImpl.java
1 package cn.itcast.restful; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import cn.itcast.domain.Person; 9 10 public class PersonServiceImpl implements IPersonService { 11 private static Map<String, Person> ps = new HashMap<String, Person>(); 12 static { 13 for(int i=0;i<5;i++){ 14 Person p = new Person(); 15 p.setId("id" + i); 16 p.setName("person" + i); 17 p.setDesc("desc" + i); 18 ps.put(p.getId(), p); 19 } 20 } 21 @Override 22 public List<Person> findAllPerson() { 23 24 return new ArrayList<Person>(ps.values()); 25 } 26 27 @Override 28 public Person findPersonById(String id) { 29 return ps.get(id); 30 } 31 32 }
多加入了 60-71 配置restful方式的webservice
cxf-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 4 xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxrs="http://cxf.apache.org/jaxrs" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://cxf.apache.org/bindings/soap 8 http://cxf.apache.org/schemas/configuration/soap.xsd 9 http://cxf.apache.org/jaxws 10 http://cxf.apache.org/schemas/jaxws.xsd 11 http://cxf.apache.org/jaxrs 12 http://cxf.apache.org/schemas/jaxrs.xsd 13 "> 14 <!-- 引入CXF Bean定义如下,早期的版本中使用 --> 15 <import resource="classpath:META-INF/cxf/cxf.xml" /> 16 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 17 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 18 19 <!-- 通过spring配置文件发布CXF的服务 --> 20 21 <!-- 第一种发布方式:没有接口的发布(简单发布) --> 22 <!-- 23 id:唯一标识 24 address:访问url 25 implementor:提供服务的类型 26 --> 27 <jaxws:endpoint id="helloService" address="/hello" 28 implementor="cn.itcast.cxf.HelloService"> 29 <!-- 加入消息拦截器 --> 30 <jaxws:inInterceptors> 31 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 32 </jaxws:inInterceptors> 33 <jaxws:outInterceptors> 34 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 35 </jaxws:outInterceptors> 36 </jaxws:endpoint> 37 38 <!-- 第二种发布方式:带有接口的发布 --> 39 <!-- 40 id:唯一标识 41 address:访问url 42 serviceClass:接口类型 43 --> 44 <jaxws:server id="hiService" address="/hi" 45 serviceClass="cn.itcast.cxf.IHiService"> 46 <jaxws:serviceBean> 47 <!-- 提供服务的实现类 --> 48 <bean class="cn.itcast.cxf.HiServiceImpl"></bean> 49 </jaxws:serviceBean> 50 51 <!-- 加入消息拦截器 --> 52 <jaxws:inInterceptors> 53 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 54 </jaxws:inInterceptors> 55 <jaxws:outInterceptors> 56 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 57 </jaxws:outInterceptors> 58 </jaxws:server> 59 60 <!-- 配置restful方式的web服务 --> 61 <bean id="ps" class="cn.itcast.restful.PersonServiceImpl"></bean> 62 <jaxrs:server id="personService" address="/p"> 63 <jaxrs:serviceBeans> 64 <ref bean="ps"/> 65 </jaxrs:serviceBeans> 66 <jaxrs:inInterceptors> 67 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 68 </jaxrs:inInterceptors> 69 <jaxrs:outInterceptors> 70 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 71 </jaxrs:outInterceptors> 72 </jaxrs:server> 73 </beans>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <!-- 通过上下文参数指定spring配置文件的位置 --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:cxf-servlet.xml</param-value> 12 </context-param> 13 <listener> 14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 15 </listener> 16 17 <!-- 配置CXF框架的核心Servlet --> 18 <servlet> 19 <servlet-name>cxf</servlet-name> 20 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 21 <!-- 通过初始化参数指定配置文件的位置 --> 22 <!-- 23 <init-param> 24 <param-name>config-location</param-name> 25 <param-value>classpath:cxf-servlet.xml</param-value> 26 </init-param> 27 --> 28 </servlet> 29 30 <servlet-mapping> 31 <servlet-name>cxf</servlet-name> 32 <url-pattern>/cxf/*</url-pattern> 33 </servlet-mapping> 34 35 <welcome-file-list> 36 <welcome-file>index.jsp</welcome-file> 37 </welcome-file-list> 38 </web-app>
发布了之后 ,cxf在上面web.xml中 被过滤处理
通过localhost:8080/项目/cxf +/persons(接口中的path路径) 访问到 服务器
通过 接口中 path的路径 直接访问提供服务的方法