服务端:
1.服务接口:
package com.demo;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
}
2.服务接口实现:
package com.demo;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
@WebService(endpointInterface="com.demo.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
return "Hello " + text;
}
public String sayHiToUser(User user) {
users.put(users.size()+1, user);
return "Hello "+ user.getName();
}
public String[] SayHiToUserList(List<User> userList) {
String[] result = new String[userList.size()];
int i=0;
for(User u:userList){
result[i] = "Hello " + u.getName();
i++;
}
return result;
}
}
3.实体类:
package com.demo;
public class User {
private String name;
private String description;
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return description
*/
public String getDescription() {
return description;
}
/**
* @param description 要设置的 description
*/
public void setDescription(String description) {
this.description = description;
}
}
4.服务发布:
package com.demo;
import javax.xml.ws.Endpoint;
public class WebServiceApp {
public void publish() {
System.out.println("web service start");
HelloWorldImpl implementor= new HelloWorldImpl();
String address="http://localhost:9292/s/webservice/helloWorld";
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}
5.外部servlet出发:
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.demo.WebServiceApp;
public class HelloWordServlet extends HttpServlet{
private static final long serialVersionUID = -5073760670530075812L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("app.xml");
WebServiceApp webServiceApp = (WebServiceApp) context.getBean("webServiceApp");
webServiceApp.publish();
}
}
6.springXML配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="webServiceApp" class="com.demo.WebServiceApp"/>
</beans>
7.webXML配置:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>TheClient</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.servlet.HelloWordServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
8.依赖jar包:
/TheService/WebContent/WEB-INF/lib/aopalliance-1.0.jar
/TheService/WebContent/WEB-INF/lib/asm-3.3.1.jar
/TheService/WebContent/WEB-INF/lib/commons-logging-1.1.1.jar
/TheService/WebContent/WEB-INF/lib/cxf-2.7.18.jar
/TheService/WebContent/WEB-INF/lib/geronimo-j2ee-management_1.1_spec-1.0.1.jar
/TheService/WebContent/WEB-INF/lib/geronimo-javamail_1.4_spec-1.7.1.jar
/TheService/WebContent/WEB-INF/lib/geronimo-jaxws_2.2_spec-1.1.jar
/TheService/WebContent/WEB-INF/lib/geronimo-jms_1.1_spec-1.1.1.jar
/TheService/WebContent/WEB-INF/lib/geronimo-servlet_3.0_spec-1.0.jar
/TheService/WebContent/WEB-INF/lib/javax.ws.rs-api-2.0-m10.jar
/TheService/WebContent/WEB-INF/lib/jaxb-api-2.2.6.jar
/TheService/WebContent/WEB-INF/lib/jaxb-impl-2.2.6.jar
/TheService/WebContent/WEB-INF/lib/jettison-1.3.7.jar
/TheService/WebContent/WEB-INF/lib/jetty-continuation-8.1.15.v20140411.jar
/TheService/WebContent/WEB-INF/lib/jetty-http-8.1.15.v20140411.jar
/TheService/WebContent/WEB-INF/lib/jetty-io-8.1.15.v20140411.jar
/TheService/WebContent/WEB-INF/lib/jetty-security-8.1.15.v20140411.jar
/TheService/WebContent/WEB-INF/lib/jetty-server-8.1.15.v20140411.jar
/TheService/WebContent/WEB-INF/lib/jetty-util-8.1.15.v20140411.jar
/TheService/WebContent/WEB-INF/lib/neethi-3.0.3.jar
/TheService/WebContent/WEB-INF/lib/serializer-2.7.1.jar
/TheService/WebContent/WEB-INF/lib/spring-aop-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-asm-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-beans-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-context-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-core-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-expression-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-jms-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-tx-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/spring-web-3.0.7.RELEASE.jar
/TheService/WebContent/WEB-INF/lib/stax2-api-3.1.4.jar
/TheService/WebContent/WEB-INF/lib/woodstox-core-asl-4.4.1.jar
/TheService/WebContent/WEB-INF/lib/wsdl4j-1.6.3.jar
/TheService/WebContent/WEB-INF/lib/wss4j-1.6.19.jar
/TheService/WebContent/WEB-INF/lib/xml-resolver-1.2.jar
/TheService/WebContent/WEB-INF/lib/xmlschema-core-2.1.0.jar
9.依赖的库(tomcat库):
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\annotations-api.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina-ant.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina-ha.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina-tribes.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\ecj-4.4.2.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\el-api.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\jasper-el.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\jasper.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\jsp-api.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\servlet-api.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-api.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-coyote.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-dbcp.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-i18n-es.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-i18n-fr.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-i18n-ja.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-jdbc.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-util.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat7-websocket.jar
C:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\websocket-api.jar
10.依赖的库(JDK库):
C:\JAVA7\jre\lib\resources.jar
C:\JAVA7\jre\lib\rt.jar
C:\JAVA7\jre\lib\jsse.jar
C:\JAVA7\jre\lib\jce.jar
C:\JAVA7\jre\lib\charsets.jar
C:\JAVA7\jre\lib\jfr.jar
C:\JAVA7\jre\lib\ext\access-bridge-64.jar
C:\JAVA7\jre\lib\ext\dnsns.jar
C:\JAVA7\jre\lib\ext\jaccess.jar
C:\JAVA7\jre\lib\ext\localedata.jar
C:\JAVA7\jre\lib\ext\sunec.jar
C:\JAVA7\jre\lib\ext\sunjce_provider.jar
C:\JAVA7\jre\lib\ext\sunmscapi.jar
C:\JAVA7\jre\lib\ext\zipfs.jar
client 端:
1.服务接口:
package com.demo;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List<User> userList);
}
2.服务接口实现:
package com.demo;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
@WebService(endpointInterface="com.demo.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String sayHi(String text) {
return "Hello " + text;
}
public String sayHiToUser(User user) {
users.put(users.size()+1, user);
return "Hello "+ user.getName();
}
public String[] SayHiToUserList(List<User> userList) {
String[] result = new String[userList.size()];
int i=0;
for(User u:userList){
result[i] = "Hello " + u.getName();
i++;
}
return result;
}
}
3.客户端调用代码:
package com.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import com.util.ContextUtil;
public class HelloWorldClient {
/*public static void main(String[] args) {
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(HelloWorld.class);
svr.setAddress("http://localhost:9191/helloWorld");
HelloWorld hw = (HelloWorld) svr.create();
User user = new User();
user.setName("Tony");
user.setDescription("test");
System.out.println(hw.sayHiToUser(user));
}*/
public void receive() {
ApplicationContext context = ContextUtil.getContext();
HelloWorld client = (HelloWorld) context.getBean("client");
User user1 = new User();
user1.setName("Tony");
user1.setDescription("test");
User user2 = new User();
user2.setName("freeman");
user2.setDescription("test");
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
String[] res = client.SayHiToUserList(userList);
System.out.println(res[0]);
System.out.println(res[1]);
}
}
4.实体类:
package com.demo;
public class User {
private String name;
private String description;
/**
* @return name
*/
public String getName() {
return name;
}
/**
* @param name 要设置的 name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return description
*/
public String getDescription() {
return description;
}
/**
* @param description 要设置的 description
*/
public void setDescription(String description) {
this.description = description;
}
}
5.service层:
package com.service;
//@Service("userService")
public class UserService {
public char[] sayHello(String string) {
return string.toCharArray();
}
}
6.访问servlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.demo.HelloWorldClient;
import com.service.UserService;
public class HelloServlet extends HttpServlet{
private static final long serialVersionUID = -5890375911020775149L;
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
HelloWorldClient helloWorldClient = new HelloWorldClient();
helloWorldClient.receive();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("Post");
PrintWriter out = resp.getWriter();
out.println(userService.sayHello("Hello,Spring.Servlet"));
}
}
7.spring context 工具类:
package com.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ContextUtil {
private static ApplicationContext context;
public static ApplicationContext getContext(){
if(context == null){
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
return context;
}
}
8.spring配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<context:annotation-config/>
<context:component-scan base-package="com.*"/>
<bean id="userService" class="com.service.UserService"/>
<jaxws:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl"
address="/helloWorld" />
<bean id="client" class="com.demo.HelloWorld" factory-bean="clientFactory"
factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.demo.HelloWorld" />
<property name="address"
value="http://localhost:9292/s/webservice/helloWorld" />
</bean>
<bean id="helloWorldClient" class="com.demo.HelloWorldClient"/>
</beans>
9.webXML配置:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>TheService</display-name>
<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>
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXFServlet</display-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>/webservice/*</url-pattern>
</servlet-mapping>
<!-- <servlet>
<servlet-name>servletToBeanProxy</servlet-name>
<servlet-class>com.servlet.ServletToBeanProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletToBeanProxy</servlet-name>
<url-pattern>/webservice/servletToBeanProxy</url-pattern>
</servlet-mapping> -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
10.其他依赖同上。