1,简介
webservice,是一种跨编程语言和跨操作系统平台的远程调用技术。
webservice三要素:soap、wsdl、uddi
2,服务端
2.1创建项目
2.2 编写服务类,并发布服务
import com.test.service.impl.HelloServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;public class Server {public static void main(String[] args) {//发布服务的工厂JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();//设置服务地址factory.setAddress("http://localhost:8000/ws/hello");//设置服务类factory.setServiceBean(new HelloServiceImpl());//添加日志输入、输出拦截器,观察soap请求、响应内容factory.getInInterceptors().add(new LoggingInInterceptor());factory.getOutInterceptors().add(new LoggingOutInterceptor());//发布服务factory.create();System.out.println("服务发布成功--");}
}# 查看wsdl说明书
http://localhost:8000/ws/hello?wsdl
3,客户端
3.1 客户端需有服务端接口
3.2 客户端调用服务
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class Client {public static void main(String[] args) {//服务接口访问地址String address="http://localhost:8000/ws/hello";//创建cxf代理工厂JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();//设置远程访问服务端地址factory.setAddress(address);//设置接口类型factory.setServiceClass(HelloService.class);//对接口生成代理对象HelloService helloService=factory.create(HelloService.class);//代理对象System.out.println(helloService.getClass());//远程访问服务端方法String content = helloService.getName("张三");System.out.println(content);}
}
4,web发布jaxws服务
4.1 创建项目
4.2 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.eclipse.maven</groupId><artifactId>server_jaxws_spring</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><!-- 要进行jaxws服务开发 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><showWarnings>true</showWarnings></configuration></plugin></plugins></pluginManagement></build>
</project>
4.3 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- cxfservlet配置 --><servlet><servlet-name>cxfservlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>cxfservlet</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping><!-- spring配置 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
4.4 applicationContext.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:cxf="http://cxf.apache.org/core"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"><!-- spring发布服务 --><jaxws:server address="/hello"><jaxws:serviceBean><bean class="com.test.service.impl.HelloServiceImpl"></bean></jaxws:serviceBean></jaxws:server>
</beans>
5,服务端调用
5.1,创建项目
5.2 ,applicationContext.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:cxf="http://cxf.apache.org/core"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"><!-- spring发布服务 --><jaxws:client id="helloService" serviceClass="com.test.service.HelloService" address="http://localhost:8080/server_jaxws_spring/ws/hello"></jaxws:client>
</beans>
5.3,测试类Client
import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Client {//注入对象@Resourceprivate HelloService helloService;@Testpublic void test() {System.out.println(helloService.getClass());System.out.println(helloService.getName("李四"));}}
6,jaxrs规范服务
6.1 创建项目
6.2 pom.xml配置
<dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-client</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-extension-providers</artifactId><version>3.1.4</version></dependency><dependency><groupId>org.codehaus.jettison</groupId><artifactId>jettison</artifactId><version>1.3.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.4.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><showWarnings>true</showWarnings></configuration></plugin></plugins></pluginManagement></build>
6.3 web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- cxfservlet配置 --><servlet><servlet-name>cxfservlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>cxfservlet</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping><!-- spring配置 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>
6.4 User类
import javax.xml.bind.annotation.XmlRootElement;/*** XmlRootElement指定对象序列化为xml或json数据时的根节点*/
@XmlRootElement(name="User")
public class User {private Integer id;private String username;private String sex;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public User() {super();}public User(Integer id, String username, String sex) {super();this.id = id;this.username = username;this.sex = sex;}@Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", sex=" + sex + "]";}}
6.5 UserService 接口
import java.util.List;import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;import com.test.entity.User;@Path("/userService")
@Produces("*/*")
public interface UserService {//表示处理请求的类型@POST@Path("/user")//服务器请求的数据格式类型@Consumes({"application/xml","application/json"})public void saveUser(User user);@PUT@Path("/user/{id}")@Consumes({"application/xml","application/json"})public void updateUser(@PathParam("id")Integer id);@GET@Path("/user/{id}")//服务器支持返回数据的格式@Produces({"application/xml","application/json"})public User findUserById(@PathParam("id")Integer id);@DELETE@Path("/user/{id}")@Consumes({"application/xml","application/json"})public void deleteUser(@PathParam("id")Integer id);
}
6.6 applicationContext.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:cxf="http://cxf.apache.org/core"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd"><!-- spring发布服务 --><jaxrs:server address="/userService"><jaxrs:serviceBeans><bean class="com.test.service.impl.UserServiceImpl"></bean></jaxrs:serviceBeans></jaxrs:server>
</beans>
7 jaxrs规范,客户端使用
7.1 创建项目
7.2 Client类
@Test
public void testSaveUser() {User user = new User(3, "王五", "女");//type指定请求的数据格式WebClient.create("http://localhost:8080/server_jaxrs_spring/ws/userService/userService/user").type(MediaType.APPLICATION_JSON).post(user);
}
@Test
public void testFindUserById() {User user = WebClient.create("http://localhost:8080/server_jaxrs_spring/ws/userService/userService/user/2").type(MediaType.APPLICATION_JSON).get(User.class);System.out.println(user);
}