1、新建一个java项目
2、导入cxf相关的jar包,并部署到项目中
3、bean类
1 package com.xiaostudy; 2 3 import javax.xml.bind.annotation.XmlRootElement; 4 5 /** 6 * @desc bean类 7 * @author xiaostudy 8 * 9 */ 10 @XmlRootElement(name = "user") 11 public class User { 12 13 private int id; 14 private String name; 15 private int age; 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 41 }
4、服务接口
1 package com.xiaostudy; 2 3 import java.util.List; 4 5 import javax.jws.WebService; 6 import javax.ws.rs.GET; 7 import javax.ws.rs.POST; 8 import javax.ws.rs.Path; 9 import javax.ws.rs.PathParam; 10 import javax.ws.rs.Produces; 11 import javax.ws.rs.core.MediaType; 12 13 /** 14 * @desc 服务器接口 15 * @author xiaostudy 16 * 17 */ 18 @WebService//表示发布服务 19 @Path("/user")//类访问数据的路径 20 public interface Test_service { 21 22 @GET//访问方式 23 //"application/json;charset=utf-8"是JSon数据类型,MediaType.APPLICATION_XML是xml数据类型 24 //两个同时用的话,get访问方式默认是xml数据类型,post默认是JSon数据类型,访问的时候可以自己设置数据类型 25 @Produces({"application/json;charset=utf-8",MediaType.APPLICATION_XML}) 26 @Path("/getUser/{name}")//方法访问数据的路径,{}表示是参数 27 public User getUser(@PathParam("name")String name);//设置参数名称 28 29 @GET//访问方式 30 @Produces("application/json;charset=utf-8")//只用JSon数据类型 31 @Path("/getUserList/{names}")//方法访问数据的路径,{}表示是参数 32 public List<User> getUserList(@PathParam("names")String... names);//设置参数名称 33 34 }
5、服务接口实现类
1 package com.xiaostudy; 2 3 import java.util.List; 4 import java.util.Vector; 5 6 /** 7 * @desc 服务器接口实现类 8 * @author xiaostudy 9 * 10 */ 11 public class Test_serviceImpl implements Test_service { 12 13 /** 14 * @desc 处理客户端的数据,并返回数据 15 * @param name 参数 16 * @return String 返回数据类型 17 */ 18 @Override 19 public User getUser(String name) { 20 User user = new User(); 21 user.setId(1111); 22 user.setName(name); 23 user.setAge(23); 24 return user; 25 } 26 27 @Override 28 public List<User> getUserList(String... names) { 29 List<User> list = new Vector<User>(); 30 for(int i = 0; i < names.length; i++) { 31 User user = new User(); 32 user.setId(1+i); 33 user.setName(names[i]); 34 user.setAge(23+i); 35 list.add(user); 36 } 37 return list; 38 } 39 40 }
6、开启服务
1 package com.xiaostudy; 2 3 import org.apache.cxf.interceptor.LoggingInInterceptor; 4 import org.apache.cxf.interceptor.LoggingOutInterceptor; 5 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; 6 7 /** 8 * @desc 开始服务器类 9 * @author xiaostudy 10 * 11 */ 12 public class MyService { 13 14 public static void main(String[] args) { 15 // JaxWsServerFactoryBean发布服务 16 JAXRSServerFactoryBean jAXRSServerFactoryBean = new JAXRSServerFactoryBean(); 17 // 设置服务实现类 18 jAXRSServerFactoryBean.setServiceBean(new Test_serviceImpl()); 19 //设置资源类 20 jAXRSServerFactoryBean.setResourceClasses(Test_serviceImpl.class); 21 // 设置服务地址 22 jAXRSServerFactoryBean.setAddress("http://127.0.0.1:9998/test"); 23 // 拦截器 24 jAXRSServerFactoryBean.getInInterceptors().add(new LoggingInInterceptor()); 25 jAXRSServerFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); 26 27 // 发布服务 28 jAXRSServerFactoryBean.create(); 29 30 } 31 32 }
7、测试是否发布成功,wsdl是w3c标准,wadl是REST的
结合spring整合发布
就是把这java的main发布转到Tomcat服务器上
applicationContext.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 8 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 9 http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 10 11 <!-- <jaxrs:server发布REST的服务 ,对JAXRSServerFactoryBean类封装--> 12 <jaxrs:server address="/test"> 13 <jaxrs:serviceBeans> 14 <ref bean="test_serviceImpl"/> 15 </jaxrs:serviceBeans> 16 </jaxrs:server> 17 18 <!-- 配置服务实现类 --> 19 <bean name="test_serviceImpl" class="com.xiaostudy.Test_serviceImpl"/> 20 </beans>
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 6 7 <display-name>ws_2_cxf_spring_server</display-name> 8 9 <!-- 设置spring的环境 --> 10 <context-param> 11 <!--contextConfigLocation是不能修改的 --> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:applicationContext.xml</param-value> 14 </context-param> 15 <listener> 16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 17 </listener> 18 19 <!-- 配置CXF的Servlet --> 20 <servlet> 21 <servlet-name>CXF</servlet-name> 22 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>CXF</servlet-name> 26 <url-pattern>/rs/*</url-pattern> 27 </servlet-mapping> 28 29 <welcome-file-list> 30 <welcome-file>index.html</welcome-file> 31 <welcome-file>index.htm</welcome-file> 32 <welcome-file>index.jsp</welcome-file> 33 <welcome-file>default.html</welcome-file> 34 <welcome-file>default.htm</welcome-file> 35 <welcome-file>default.jsp</welcome-file> 36 </welcome-file-list> 37 </web-app>