一.WebService应用场景
- 跨互联网调用:通过WebService可以跨互联网调用其他程序
- 跨语言:不同语言之间也可以通过WebService进行调用
- 集成多应用:一个企业里使用了多个软件,比如:财务系统,和咱们的SaaS-Export对接
- 多程序复用:多个系统中都有用户,那么只需要一个用户就可以了,登录之后,通过调用WebService也同时登录
二.WebService的两个规范
jax-ws
访问路径例如:http://localhost:8034/ws/weather?wsdl
ws:为代码中加密的路径,weather为访问路径
- soap:传输协议
- wsdl:说明书,xml格式存储的,不是给人看的给机器看的
- uddi:目录(包含所有可以调用的WebService方法)
jax-rs
- rest风格调用
- CXF:磁悬浮
三.WebService入门案例(jax-ws)
1.创建服务提供者模块(转成web工程)
1.1 引入依赖
1.2 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
//配置文件的路径
<param-value>classpath:applicationContext-cxf-server.xml</param-value>
</context-param>
<!-- CXF框架的核心控制器:CXFServlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<!--前台页面访问时 ws的才可以访问-->
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
1.3 配置applicationContext-cxf-server.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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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
">
<jaxws:server address="/weather">
<jaxws:serviceBean>
<bean class="cn.itcast.service.impl.WeatherServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
1.4 编写service以及impl配置tomcat访问
2.创建服务调用这模块
2.1 使用jdk下面的wsimport.exe进行代码生成
2.2 实现步骤
1.新建模块找到模块的java目录
2.打开cmd窗口在cmd跳转到java目录
3.在cmd窗口运行 生成代码
D:\Install\Java\work\demo\webServiceDemo\jar_ws_cunsomer\src\main\java>wsimport.exe -s . http://localhost:8034/ws/weather?wsdl
-s <directory> 指定放置生成的源文件的位置
. 为当前目录
路径为服务提供者的模块路径
2.3 配置文件
<?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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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
">
<jaxws:client id="weatherService" address="http://localhost:8034/ws/weather?wsdl" serviceClass="cn.itcast.service.impl.WeatherService">
<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:client>
</beans>
2.4编写测试类
package cn.itcast;
import cn.itcast.service.impl.WeatherService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author cyy
* @date 2020/3/20 19:11
*/
public class test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-cxf-client.xml");
WeatherService weatherService = (WeatherService) context.getBean("weatherService");
String cityName = weatherService.getWeatherByCityName("北京");
//调用完会返回万里无云
System.out.println(cityName);
}
}
四.WebService入门案例(jax-rs)
1.创建服务提供者模块
1.1引入依赖
<dependencies>
<!-- cxf 进行rs开发 必须导入 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志引入 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<!-- 客户端 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 扩展json提供者 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 转换json工具包,被extension providers 依赖 -->
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.7</version>
</dependency>
<!-- spring 核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- spring web集成 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- spring 整合junit -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- junit 开发包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
1.2web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>jax_rs_provider</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF的核心控制器 -->
<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>
</web-app>
1.3applicationContext-cxf.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:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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
">
<jaxrs:server address="/userService" serviceClass="cn.itcast.service.UserServiceImpl">
</jaxrs:server>
</beans>
1.4 写domain和service
- domain 上面的
@XmlRootElement(name = "User")
提供一个实体类的xml - service
/**
* @Path:调用的路径
* http://localhost:9082/ws/userService/user
*
* @Produces 传出格式 *|* 代表xml 和 json
* @POST\@PUT\@GET\@DELETE 方法类型
* @Consumes 传入格式 *|* 代表xml 和 json
*/
@Produces("*/*")
public interface IUserService {
@POST
@Path("/user")
@Consumes({ "application/xml", "application/json" })
public void saveUser(User user);
@PUT
@Path("/user")
@Consumes({ "application/xml", "application/json" })
public void updateUser(User user);
@GET
@Path("/user")
@Produces({ "application/xml", "application/json" })
public List<User> findAllUsers();
@GET
@Path("/user/{id}")
@Consumes("application/xml")
@Produces({ "application/xml", "application/json" })
public User finUserById(@PathParam("id") Integer id);
@DELETE
@Path("/user/{id}")
@Consumes({"application/xml", "application/json"})
public void deleteUser(@PathParam("id") Integer id);
}
2.创建服务调用者模块
2.1 引入依赖
2.2 根据提供者编写实体类
2.3 编写测试类
package cn.itcast.test;
import cn.itcast.domain.UserDomain;
import org.apache.cxf.jaxrs.client.WebClient;
/**
* @author cyy
* @date 2020/3/22 10:19
*/
public class Test {
public static void main(String[] args) {
// 1.创建一个wc
WebClient webClient = WebClient.create("http://localhost:9083/ws/userService/user/1");
// 2.通过wc进行get方法的调用
UserDomain userDomain = webClient.get(UserDomain.class);
// 3.返回结果进行打印
System.out.println(userDomain.toString());
}
}
五.总结
如果提供者是jax-ws, http://localhost:9081/ws/weather?wsdl 这个地址必须是提供方告诉你的
- 通过提供方给你的地址用wsimport.exe生成代码
- 配置文件重点关注:address(服务提供者给你的地址);serviceClass(生成的代码里有接口文件,这是接口文件导入的路径)
- 创建调用类,使用上下文加载配置文件,获取里面的Bean,生成接口,调用接口里的方法
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-cxf-client.xml");
WeatherService weatherService = (WeatherService) context.getBean("weatherService");
String cityName = weatherService.getWeatherByCityName("北京");
System.out.println(cityName);
如果提供者是jax-rs,调用jax-rs之前,需要向服务提供者要接口文档
- 引入依赖
- 根据返回值创建实体类
- 创建测试类,通过webClient调用接口
public static void main(String[] args) {
// 1.创建一个wc
WebClient webClient = WebClient.create("http://localhost:9083/ws/userService/user/1");
// 2.通过wc进行get方法的调用
UserDomain userDomain = webClient.get(UserDomain.class);
// 3.返回结果进行打印
System.out.println(userDomain.toString());
}