java ee5中开发web service

在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 >
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

peihexian

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值