webservice笔记

1,简介

webservice,是一种跨编程语言和跨操作系统平台的远程调用技术。
webservice三要素:soap、wsdl、uddi

soap提供标准的RPC(远程调用技术)方法来调用WebService
soap协议是基于HTTP协议的,也是基于XML的,XML是SOAP的数据编码方式。
SOAP协议=HTTP协议+XML数据格式

WSDL是WebService使用说明书

UDDI是一个目录服务

2,服务端
2.1创建项目
在这里插入图片描述
在这里插入图片描述

2.2 编写服务类,并发布服务

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.test.server.service.impl.HelloServiceImpl;

public class Server {
    public static void main(String[] args) {
    	String address = "http://localhost:8000/ws/hello";
//        server01(address);
        server02(address);
        
        System.out.println("服务发布成功--");
    }

	private static void server02(String address) {
		Endpoint.publish(address, new HelloServiceImpl());
	}

	private static void server01(String address) {
		//发布服务的工厂
        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();
	}
}

# 查看wsdl说明书
http://localhost:8000/ws/hello?wsdl

2.3 WSDL结构

service:服务试图,WebService的服务端点
binding:webService的通信协议,还描述WebService的方法、输入、输出。
portType:描述WebService可执行的操作,通过binding执行portType
message:描述服务发布的方法。包括参数,返回值等
types:定义webservice中使用的数据类型。

3,客户端
3.1 客户端需有服务端接口
在这里插入图片描述
3.2 客户端调用服务

在cmd中生成客户端代码(-d:生成代码的位置,-p:客户端代码的包名,服务端的地址)
wsdl2java -d E:\study\webservice -p http://localhost:8000/ws/hello?wsdl

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.test.server.service.impl.HelloService;
import com.test.server.service.impl.HelloServiceImplService;

public class Client {
	public static void main(String[] args) throws Exception {
//		test01();
//		test02();
		test03();
				
	}

	private static void test03() throws MalformedURLException {
		//设置服务地址(服务wsdl地址)
		URL url = new URL("http://localhost:8000/ws/hello?wsdl");
		//设置服务名称和命名空间
		//第一个参数:targetNamespace
		//第二个参数:服务视图
		QName qName = new QName("http://impl.service.server.test.com/", "HelloServiceImplService");
		//创建服务视图
		Service service = Service.create(url, qName);
		//得到服务的实现类
		HelloService helloService = service.getPort(HelloService.class);
		String name = helloService.getName("小明");
		System.out.println(name);
	}

	private static void test02() {
		//创建服务视图(service节点name的属性)
		HelloServiceImplService service = new HelloServiceImplService();
		//获得服务的实现类(protType节点的name的属性)
		HelloService helloService = service.getPort(HelloService.class);
		//调用接口中的方法
		String name = helloService.getName("李四");
		System.out.println(name);
	}
	
	private static void test01() {
		//服务接口访问地址
        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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/core
       http://cxf.apache.org/schemas/core.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
       ">

    <!-- 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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/core
       http://cxf.apache.org/schemas/core.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
       ">

    <!-- 配置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 {
	//注入对象
	@Resource
	private HelloService helloService;
	
	@Test
	public 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;
	}
	@Override
	public 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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/core
       http://cxf.apache.org/schemas/core.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
       ">

    <!-- 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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值