java ee5中开发web service

来源:互联网 发布:腾讯视频 网络电影 编辑:程序博客网 时间:2024/06/04 19:25

在java ee5之前,要使用java技术开发web service,需要借助第三方的框架xfire,axis,cxf等,现在借助最新的java ee5技术,不再需要向项目中引入大量第三方的jar文件,有jdk 6就够了。

 

使用步骤:

1.web service代码

package com.test;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;


@WebService(targetNamespace="http://demo.test.org/hello")
@SOAPBinding(style=Style.RPC)


public class TestService {

 

 @WebMethod(operationName="Hello")
 public String sayHello(@WebParam(name="name")String msg){
  return "Hello :"+msg;
 }


}

代码很简单,不需要实现什么接口,也不需要继承自什么特殊的超类,与普通的java类相比,最大的区别是大量的使用了注释技术。注意上面的@SOAPBinding(style=Style.RPC)不能省略,否则会报异常:

严重: StandardWrapper.Throwable
com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class com.test.jaxws.SayHello is not found. Have you run APT to generate them?

 

严重: Servlet /m4 threw load() exception
com.sun.xml.internal.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class com.test.jaxws.SayHello is not found. Have you run APT to generate them?

 

2.发布服务

   和以前的web service开发技术相比较,现在需要用程序代码发布一下自己开发的web serivce,新建一个serlvet,在init代码中写发布代码

 

package com.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.xml.ws.Endpoint;

 

public class WebServiceLoader extends HttpServlet {

 

 private static final long serialVersionUID = 1L;

 public WebServiceLoader() {
  super();
 }

 public void destroy() {
  super.destroy();
 }

 public void init() throws ServletException {
  System.out.println("Web Service发布中......");
  Endpoint.publish("http://0.0.0.0:8081/Hello", new TestService());
  System.out.println("Web Service发布完成");
 }

}

注意端口号问题,上面的示例代码中使用的是8081端口,如果发布到已经被占用的端口将会失败(如tomcat本身工作在8080,如果再发布到8080就会失败!)

 

3.配置servlet自动加载

 

打开web.xml,配置servlet为自动加载

 

  <servlet>
    <servlet-name>WebServiceLoader</servlet-name>
    <servlet-class>com.test.WebServiceLoader</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

4.访问wsdl

   部署程序,启动容器,如tomcat等,然后打开浏览器,访问 http://localhost:8081/Hello?wsdl  可以看到wsdl描述符。

 

  <?xml version="1.0" encoding="UTF-8" ?>
- <!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. 
  -->
- <!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. 
  -->
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://demo.test.org/hello" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://demo.test.org/hello" name="TestServiceService">
  <types />
- <message name="Hello">
  <part name="name" type="xsd:string" />
  </message>
- <message name="HelloResponse">
  <part name="return" type="xsd:string" />
  </message>
- <portType name="TestService">
- <operation name="Hello" parameterOrder="name">
  <input message="tns:Hello" />
  <output message="tns:HelloResponse" />
  </operation>
  </portType>
- <binding name="TestServicePortBinding" type="tns:TestService">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
- <operation name="Hello">
  <soap:operation soapAction="" />
- <input>
  <soap:body use="literal" namespace="http://demo.test.org/hello" />
  </input>
- <output>
  <soap:body use="literal" namespace="http://demo.test.org/hello" />
  </output>
  </operation>
  </binding>
- <service name="TestServiceService">
- <port name="TestServicePort" binding="tns:TestServicePortBinding">
  <soap:address location="http://localhost:8081/Hello" />
  </port>
  </service>
  </definitions>