当前位置: 代码迷 >> Web前端 >> 怎么建立一个WebService项目
  详细解决方案

怎么建立一个WebService项目

热度:127   发布时间:2012-07-25 09:43:05.0
如何建立一个WebService项目
(1) 首先新建一个项目,导入Spring和axis包
(2) 修改web.xml文件
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
  <display-name>contextConfigLocation</display-name>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>
<servlet>
  <servlet-name>axis</servlet-name>
  <servlet-class>
    org.apache.axis.transport.http.AxisServlet
  </servlet-class>
  <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>axis</servlet-name>
  <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

(3) 建立applicationContext.xml
<bean id="MathBean" class="test.remote.MathServiceImpl"/>

(4) 配置一个wsdd文件,名称为server-config.wsdd,放在webRoot/WEB-INF/
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <globalConfiguration>
    <parameter name="adminPassword" value="admin"/>
    <parameter name="sendXsiTypes" value="true"/>
    <parameter name="sendMultiRefs" value="true"/>
    <parameter name="sendXMLDeclaration" value="true"/>
    <parameter name="axis.sendMinimizedElements" value="true"/>
    <requestFlow>
      <handler type="java:org.apache.axis.handlers.JWSHandler">
        <parameter name="scope" value="session"/>
      </handler>
      <handler type="java:org.apache.axis.handlers.JWSHandler">
        <parameter name="scope" value="request"/>
        <parameter name="extension" value=".jwr"/>
      </handler>
    </requestFlow>
  </globalConfiguration>
  <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
  <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
  <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
  <!-- 需要修改的配置start -->
  <service name="MathService" provider="java:RPC">
    <parameter name="allowedMethods" value="*"/>
    <parameter name="className" value="test.remote.JaxRpcMathService"/>
    <beanMapping qname="wsdemo:MathVO" xmlns:wsdemo="urn:wsdemo" languageSpecificType="java:test.vo.MathVO"/>
  </service>
  <!-- 需要修改的配置end -->
  <service name="AdminService" provider="java:MSG">
    <parameter name="allowedMethods" value="AdminService"/>
    <parameter name="enableRemoteAdmin" value="false"/>
    <parameter name="className" value="org.apache.axis.utils.Admin"/>
    <namespace>http://xml.apache.org/axis/wsdd/</namespace>
  </service>
  <service name="Version" provider="java:RPC">
    <parameter name="allowedMethods" value="getVersion"/>
    <parameter name="className" value="org.apache.axis.Version"/>
  </service>
  <transport name="http">
    <requestFlow>
      <handler type="URLMapper"/>
      <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
    </requestFlow>
  </transport>
  <transport name="local">
    <responseFlow>
      <handler type="LocalResponder"/>
    </responseFlow>
  </transport>
</deployment>

(5) 编写JaxRpcMathService类
public class JaxRpcMathService extends ServletEndpointSupport implements MathService {
  private MathService mathService;
  protected void onInit() {
    this.mathService = (MathService) getWebApplicationContext().getBean(MathService.BEAN_ID);
  }
  public int add(int x, int y) {
    return mathService.add(x,y);
  }
  public int subtract(int x, int y) {
    return mathService.subtract(x,y);
  }
  public MathVO testVO(MathVO vo) {
    return mathService.testVO(vo);
  }
}

(6) 编写MathService接口类
public interface MathService {
  String BEAN_ID = "MathBean";
  public int add(int x, int y);
  public int subtract(int x, int y);
  public MathVO testVO(MathVO vo);
}

(7) 编写MathServiceImpl实现类
public class MathServiceImpl implements MathService{
  public int add(int x, int y) {
    return x+y;
  }
  public int subtract(int x, int y) {
    return x-y;
  }
  public MathVO testVO(MathVO vo) {
    System.out.println(vo.toString());
    return vo;
  }
}

(8) 客户端代码生成
通过eclipse打开Open Run Dialog

在main class中填入org.apache.axis.wsdl.WSDL2Java

在arguments中填入参数
-p src.com.test.vo http://localhost:8080/wsdemo1/ws/MathService?WSDL
启动tomcat,然后点击run生成代码

(9) 代码生成完毕后编写客户端调用TestMathService类
public class TestMathService {
  public static void main(String[] args) {
    JaxRpcMathServiceService service = new JaxRpcMathServiceServiceLocator();
    JaxRpcMathService client = service.getMathService();
    System.out.println(client.add(1, 2));
  }
}
  相关解决方案