在struts2中加入java的web service示例
来源:互联网 发布:iai电缸软件 编辑:程序博客网 时间:2024/05/16 11:33
前两天做项目,遇到一个需求,需要去另一个项目中调用一个接口,当时,我就考虑到了java web service技术,原因是在以前的项目中用到过,有示例,想到这里,真让人着急,会用却不知其中真正原理,对一个程序猿来说,也是一个悲剧,所以有时间了,我也会看一下,java web服务构建与运行,不磨叽了,直接上代码。
服务端:
1、先建立一个接口InterfaceService ,如下
package test;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, parameterStyle = ParameterStyle.WRAPPED)
public interface InterfaceService {
@WebMethod
List<Object> getObjs();
}
2、建立一个实现接口的子类InterfaceServiceImpl ,
@WebService(targetNamespace = "http://ws.test.com.cn/", serviceName = "InterfaceService", portName = "InterfacePort", endpointInterface = "test.InterfaceService")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED)
public class InterfaceServiceImpl implements InterfaceService {
@Override
public List<Object> getObjs() {
//这里写自己的业务逻辑
system.out.println("--------test-----------");
return null;
}
}
3、先说一下如何配置applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean name="wsExporter"class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:9090/services/" />
</bean>
<bean id="InterfaceService" class="test.InterfaceServiceImpl" />
</beans>
4、将applicationContext.xml的配置文件加入到项目web.xml中,但是由于加入spring相关的文件,所以需要加入几个spring相关的jar包,spring-beans-4.0.M2.jar、spring-context-4.0.0.M2.jar、spring-core-4.0.0.M2.jar、spring-expression-4.0.0.M2.jar、spring-web-4.0.0.M2.jar,同时web.xml文件的配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml //说明applicationContext.xml文件位于WEB-INF下,当然也可以是其它路径。
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
5、现在可以编写一个访问客服端,如下:
class testws{
public static void main(String[] args ){
testObjs();
}
private static void testObjs() throws MalformedURLException {
URL url = new URL("http://localhost:9090/services/InterfaceService?wsdl");
QName qname = new QName("http://ws.test.com.cn/","InterfaceService");
Service service = Service.create(url2, qname2);
InterfaceService resCollect = service2.getPort(InterfaceService.class);
List<Object> objs = resCollect2.getObjs();
System.out.println(objs);
}
}
6 现在启动tomcat,可以通过客户端进行web service访问了,在实际工作中,是将web service服务端的相关接口和vo类打成一个jar包,加入另一个项目中,这样另一个项目可以通过jar包的内容进行web service的访问。
注:好久没有写帖子了,可能写的不是很好,望各位路过的大牛,不喜勿喷!
- 在struts2中加入java的web service示例
- 在Spring中加入Struts2
- 在java web项目中加入ueditor在线编辑器
- 在基于Struts构架的Java Web项目中加入ICTCLAS分词
- 关于在基于Struts构架的Java Web项目中加入ICTCLAS分词两点说明
- PL/SQL调用JAVA发布的Web Service示例
- SSH的web项目中加入jersey框架实现web service
- 在struts2中加入装饰器sitemesh
- myeclipse web service 示例(java project 转 web service)
- Asp.Net中应用Web Service示例
- 在java项目中配置web service服务
- 在java项目中配置web service服务
- 为自己的Web Service程序加入Web Server
- HTML中加入在Eclipse中写的java程序
- 在web.xml中加入过滤器
- 在Web工程中加入favicon.ico
- 在Spring boot中加入web.xml
- web service的基础教程 web Service java
- shell脚本中简单的函数示例
- Internet起源
- OpenGL绘制球形相关学习
- 初步认识Internet
- Sublime Text3 快捷键汇总及设置快捷键配置环境变量
- 在struts2中加入java的web service示例
- 【Android】Android国际化
- Python copy模块
- 排序算法之快速排序
- windows + visual studio 2010 配置SVN
- Preconditions
- 在HTML中输出文本需要保留空格和换行符时需要加 <pre>
- 【机器学习基础】机器学习基础引入
- SearchView(二)