博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebService简单案例
阅读量:6329 次
发布时间:2019-06-22

本文共 12628 字,大约阅读时间需要 42 分钟。

hot3.png

服务端:

1.服务接口:

package com.demo;import java.util.List;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface HelloWorld {     String sayHi(@WebParam(name="text")String text);     String sayHiToUser(User user);     String[] SayHiToUserList(List
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
users = new LinkedHashMap
(); 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
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配置:

7.webXML配置:

TheClient
index.html
contextConfigLocation
classpath:app.xml
org.springframework.web.context.ContextLoaderListener
helloServlet
com.servlet.HelloWordServlet
helloServlet
/HelloServlet

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.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina-ant.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina-ha.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina-tribes.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\catalina.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\ecj-4.4.2.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\el-api.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\jasper-el.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\jasper.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\jsp-api.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\servlet-api.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-api.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-coyote.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-dbcp.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-i18n-es.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-i18n-fr.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-i18n-ja.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-jdbc.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat-util.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\tomcat7-websocket.jarC:\VenusTools2013\appserver\apache-tomcat-7.0.57\lib\websocket-api.jar

10.依赖的库(JDK库):

 

C:\JAVA7\jre\lib\resources.jarC:\JAVA7\jre\lib\rt.jarC:\JAVA7\jre\lib\jsse.jarC:\JAVA7\jre\lib\jce.jarC:\JAVA7\jre\lib\charsets.jarC:\JAVA7\jre\lib\jfr.jarC:\JAVA7\jre\lib\ext\access-bridge-64.jarC:\JAVA7\jre\lib\ext\dnsns.jarC:\JAVA7\jre\lib\ext\jaccess.jarC:\JAVA7\jre\lib\ext\localedata.jarC:\JAVA7\jre\lib\ext\sunec.jarC:\JAVA7\jre\lib\ext\sunjce_provider.jarC:\JAVA7\jre\lib\ext\sunmscapi.jarC:\JAVA7\jre\lib\ext\zipfs.jar

client 端:

1.服务接口:

package com.demo;import java.util.List;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface HelloWorld {     String sayHi(@WebParam(name="text")String text);     String sayHiToUser(User user);     String[] SayHiToUserList(List
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
users = new LinkedHashMap
(); 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
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
userList = new ArrayList
(); 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配置:

9.webXML配置:

TheService
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
helloServlet
com.servlet.HelloServlet
helloServlet
/HelloServlet
CXFServlet
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/webservice/*
index.html

10.其他依赖同上。

转载于:https://my.oschina.net/wliming/blog/682907

你可能感兴趣的文章
UOJ#179. 线性规划(线性规划)
查看>>
整合spring cloud云架构 - SSO单点登录之OAuth2.0登录认证(1)
查看>>
Isolation Forest原理总结
查看>>
windows的服务中的登录身份本地系统账户、本地服务账户和网络服务账户修改
查看>>
JAVA中循环删除list中元素的方法总结
查看>>
redis 安装
查看>>
C# tips ---值类型的装箱和拆箱
查看>>
SQL some any all
查看>>
电子书下载:Programming Windows Identity Foundation
查看>>
有理想的程序员必须知道的15件事
查看>>
用于测试的字符串
查看>>
财付通和支付宝资料收集
查看>>
PHPCMS V9数据库表结构分析
查看>>
『原创』+『参考』基于PPC的图像对比程序——使用直方图度量
查看>>
理解 IEnumerable 与 IEnumerator
查看>>
NHibernate 2.0 Beta 1 Released和一些工具
查看>>
【每天一个Linux命令】12. Linux中which命令的用法
查看>>
软件接口数据一致性机制
查看>>
微服务架构介绍和RPC框架对比
查看>>
Debian下使用OpenLDAP 管理端
查看>>