在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的访问。


注:好久没有写帖子了,可能写的不是很好,望各位路过的大牛,不喜勿喷!





    

0 0
原创粉丝点击