开发基于CXF的 RESTful WebService web 项目 webservice发布

时间:2022-06-21 08:25:21

配置步骤

开发基于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

 package cn.itcast.domain;

 import java.io.Serializable;

 import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
  //通过注解,在网上序列号成xml或者json方式
@XmlRootElement(name = "Person")
public class Person implements Serializable {
private String id;
private String name;
private String desc;
@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="desc")
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Person [desc=" + desc + ", id=" + id + ", name=" + name + "]";
} }

IPersonService.java

 package cn.itcast.restful;

 import java.io.Serializable;
import java.util.List; import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import cn.itcast.domain.Person;
//@Produces( { MediaType.APPLICATION_XML }) xml格式,以下是json格式 传
@Produces( { MediaType.APPLICATION_JSON })
public interface IPersonService extends Serializable { @GET
@Path(value="/persons/{id}")
public Person findPersonById(
@PathParam("id")
String id); @GET
@Path(value="/persons")
public List<Person> findAllPerson();
}

PersonServiceImpl.java

 package cn.itcast.restful;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import cn.itcast.domain.Person; public class PersonServiceImpl implements IPersonService {
private static Map<String, Person> ps = new HashMap<String, Person>();
static {
for(int i=0;i<5;i++){
Person p = new Person();
p.setId("id" + i);
p.setName("person" + i);
p.setDesc("desc" + i);
ps.put(p.getId(), p);
}
}
@Override
public List<Person> findAllPerson() { return new ArrayList<Person>(ps.values());
} @Override
public Person findPersonById(String id) {
return ps.get(id);
} }

多加入了 60-71 配置restful方式的webservice

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: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://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- 通过spring配置文件发布CXF的服务 --> <!-- 第一种发布方式:没有接口的发布(简单发布) -->
<!--
id:唯一标识
address:访问url
implementor:提供服务的类型
-->
<jaxws:endpoint id="helloService" address="/hello"
implementor="cn.itcast.cxf.HelloService">
<!-- 加入消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint> <!-- 第二种发布方式:带有接口的发布 -->
<!--
id:唯一标识
address:访问url
serviceClass:接口类型
-->
<jaxws:server id="hiService" address="/hi"
serviceClass="cn.itcast.cxf.IHiService">
<jaxws:serviceBean>
<!-- 提供服务的实现类 -->
<bean class="cn.itcast.cxf.HiServiceImpl"></bean>
</jaxws:serviceBean> <!-- 加入消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:server> <!-- 配置restful方式的web服务 -->
<bean id="ps" class="cn.itcast.restful.PersonServiceImpl"></bean>
<jaxrs:server id="personService" address="/p">
<jaxrs:serviceBeans>
<ref bean="ps"/>
</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>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置CXF框架的核心Servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通过初始化参数指定配置文件的位置 -->
<!--
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</init-param>
-->
</servlet> <servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

发布了之后  ,cxf在上面web.xml中 被过滤处理

通过localhost:8080/项目/cxf  +/persons(接口中的path路径) 访问到 服务器

通过 接口中 path的路径 直接访问提供服务的方法