一 前言
前面有一篇有关webService是转发他人的,这里自己整理一篇自己的案例,加深一下印象。话不多说,下面直接开始记录
二 webService服务端
1.创建一个maven项目
添加需要的依赖,pom文件如下所示:
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.web</groupId>
<artifactId>webserviceServer</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webservice Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<finalName>webserviceServer</finalName>
</build>
</project>
2.新建webService接口以及其实现类
WeatherService:
package com.chp.webservice.server;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface WeatherService {
public String getWeather(@WebParam(name = "place") String place);
}
WeatherServiceImpl:
package com.chp.webservice.server;
import javax.jws.WebService;
@WebService(endpointInterface="com.chp.webservice.server.WeatherService")
public class WeatherServiceImpl implements WeatherService{
@Override
public String getWeather(String place) {
return "天气晴朗";
}
}
3.配置spring.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!--=============== 实现类的bean,需要spring注入 ============================ -->
<bean id="weatherServiceImpl" class="com.chp.webservice.server.WeatherServiceImpl" />
<jaxws:endpoint id="getWeather" implementor="#weatherServiceImpl" address="/getWeather" />
</beans>
4.配置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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern><!--路径/项目名/service/getWeather?wsdl -->
</servlet-mapping>
<display-name>hello world!</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
启动服务器,在浏览器输入地址:http://localhost:8080/webserviceServer/service/getWeather?wsdl
出现如下图代表新建成功
三 webService客户端
新建一个客户端webserviceClient的maven项目,右键新建webService的客户端
输入路径地址:http://localhost:8080/webserviceServer/service/getWeather?wsdl
就是服务端的访问路径
选择指定的位置
完成生成代码如下
测试:
public class TestService {
public static void main(String[] args) throws RemoteException {
WeatherServiceProxy ws=new WeatherServiceProxy();
String a=ws.getWeather("杭州");
System.out.println(a);
}
}
输出结果则成功