新建一个web maven工程
1、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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.agelong.taozhugong</groupId>
<artifactId>agelong-apollo-framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>agelong-webservice</artifactId>
<packaging>war</packaging>
<name>agelong-webservice</name>
<description>WS组件</description>
<build>
<finalName>agelong-webservice</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.8.v20121106</version>
<configuration>
<webApp>
<contextPath>/CompanyPro</contextPath>
</webApp>
</configuration>
<!--
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies> -->
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.agelong.taozhugong</groupId>
<artifactId>agelong-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
</project>
2、配置web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Test WebService</display-name>
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
-->
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<!--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>-->
<servlet>
<servlet-name>WSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WSServlet</servlet-name>
<url-pattern>/CompanyService</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/index.jsp</location>
</error-page>
</web-app>
3、新建webservice接口
@WebService
public interface CompanyService {
@WebMethod
public String demo(String companyName,String licenceNum,String licenceAddress);;
}
4、实现上述接口
@WebService(endpointInterface="com.winchampion.taozhugong.ws.CompanyService")
public class CompanyServiceImpl implements CompanyService {
@Override
public String demo(String companyName, String licenceNum,
String licenceAddress) {
System.out.println("DEMO");
return "DEMO";
}
}
5、配置sun-jaxws.xml
该配置文件跟web.xml文件在同一目录下
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="CompanyService"
implementation="com.winchampion.taozhugong.ws.CompanyServiceImpl"
url-pattern="/CompanyService" />
</endpoints>
运行
1、jetty配置
2、运行后访问
http://localhost:8080/CompanyPro/CompanyService
出现如下画面
客户端调用
1、生成客户端代码
然后用命令提示符进入jdk的bin目录下,运行一下命令:
wsimport -p com.winchampion.taozhugong.ws.client -d c:\IDE\zzy -keep http://localhost:8080/CompanyPro/CompanyService?wsdl
之后会生成客户端代码,com.winchampion.credit.core.ws.client是生成的目标代码包名,D:\IDE\zzy是指存放目标代码的路径
2、将java文件拷贝至客户端
2、调用接口
public static void main(String[] args) {
CompanyServiceImplService service=new CompanyServiceImplService();
System.out.println(service);
com.winchampion.taozhugong.ws.client.CompanyService client= service.getCompanyServiceImplPort();
System.out.println(client.demo("", "", ""));
}
demo是接口中的方法名,client是接口实例