4,把POJO或EJB搞成(呵呵)Web Service
可以将POJO或无状态EJB通过annotation的方式搞成Web Service。这些annotation包括@WebService,@WebMethod,WebParam
及@WebResult等。其属性都有却省值,但本文觉得有些参数不应该用缺省值。对本文的例子而言,@SOAPBinding 的属性之一
parameterStyle=SOAPBinding.ParameterStyle.BARE 是必须的,因为缺省值不合适。另外,@WebParam @WebResult
的属性targetNamespace也不能用缺省,而必须符合本文service Schema 的定义:"http://www.t50.com/portable"。
这些annotation将在部署时被服务器用来产生必要的service组件。察看WSDL将有助于理解这些annotation的参数的意义。
POJO通过annotation的方式搞成Web Service,该POJO应打包在.war中;无状态EJB通过annotation的方式搞成Web Service,
该ejb应打包在ejb-jar中。
下面是两个简单的例子:一个是本文前面提到的service GetPerson的pojo实现;一个是ejb的例子。pojo实现定义了business
interface,annotation 主要发生在该interface上。
pojo实现的business interface:-------------
{code}
package t50.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.t50.portable.GetPersonRequest;
import com.t50.portable.GetPersonResponse;
/**
* A web service business interface
* <br/><br/>
* Would be implemented by container as a Servlet and therefore should be packaged in a war, e.g. t50.war
* <br/><br/>
* If parameter style is ParameterStyle.WRAPPED, it would expect Java built-in parameters and you could use
* this url to test it with GlassFish: http://localhost:8080/t50/pojoWs?tester
* <br/><br/>
* However, this implementation uses ParameterStyle.BARE and it expects single request parameter. Therefore,
* we really need a client program to send SOAP messages wrapping in our request payload to invoke the web service.
* <br/><br/>
* For a Stateless EJB exposed as a web service, it should be packaged in an ejb-jar, e.g. t50-ejb.jar
* <br/>
* @see t50.service.tes.SoapClient for a simple SOAP client.
* @see t50.ejb.EJBYello for an EJB exposing similar method as web service.
* <br/>
* @author JWang
*/
@WebService
@SOAPBinding(
style = SOAPBinding.Style.DOCUMENT, // this is default value, rarely needs to change.
use = SOAPBinding.Use.LITERAL, // this is default value, rarely needs to change.
//parameterStyle = SOAPBinding.ParameterStyle.WRAPPED // the default value
parameterStyle=SOAPBinding.ParameterStyle.BARE // when using your custom classes for @WebParam
)
public interface EchoWebService {
@WebMethod
@WebResult( name="getPersonResponse",
targetNamespace="http://www.t50.com/portable",
partName="getPersonResponse" )
public GetPersonResponse sayHello( @WebParam(name="getPersonRequest",
targetNamespace="http://www.t50.com/portable",
partName="getPersonRequest" )
GetPersonRequest request);
}
{code}
pojo实现---------------
{code}
package t50.service;
import javax.jws.WebService;
import org.apache.log4j.Logger;
import com.t50.portable.GetPersonRequest;
import com.t50.portable.GetPersonResponse;
import com.t50.portable.Person;
/**
* web service implementation class - would be exposed as a Servlet
*
* @author JWang
*
*/
@WebService(serviceName="pojoEcho", endpointInterface="t50.service.EchoWebService")
public class EchoWebServiceImp implements EchoWebService {
private static final Logger log = Logger.getLogger(EchoWebServiceImp.class);
public GetPersonResponse sayHello(GetPersonRequest request) {
if(request == null) {
log.debug(EchoWebServiceImp.class.getName() + ":-( service request=null");
return new GetPersonResponse();
}
// int pid = request.getPid();
// log.debug("Getting person for pid: " + pid);
Person person = request.getPerson();
if(person == null) {
log.debug("john, it's no good that person in request is null. set age to 89");
person = new Person();
person.setFirstName("john");
person.setLastName("wang");
person.setAge(89);
}
else {
log.debug("john, everything goes well, lets set person's age to 30001");
person.setAge(30001); // this guy lives a pretty long life!
}
// construct response to return
GetPersonResponse response = new GetPersonResponse();
response.setPerson(person);
return response;
}
}
{code}
ejb Remote Interface ----------------
{code}
package t50.ejb;
import javax.ejb.Remote;
@Remote
public interface YelloRemote {
public String ejbHello(String s, int n);
}
{code}
ejb Implementation ------------------
{code}
package t50.ejb;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
* A Stateless EJB exposed as a web service, it should be packaged in a jar, e.g. t50-ejb.jar
* Use this url to test it with GlassFish: http://localhost:8080/EJBYelloService/ejbWs?tester
*
* If a POJO exposed as a web service, it should be packaged in a war, e.g. t50.war.
* Since it's implemented by the container as a servlet.
*
* @author JWang
*
*/
@Stateless
@WebService(name="ejbWs")
public class EJBYello implements YelloRemote {
@WebMethod
@WebResult(name="ejbHiBack")
public String ejbHello( @WebParam(name="username")
String aname,
@WebParam(name="userage")
int aage) {
return "An EJB exposed as web service. params: username=" + aname + ", userage=" + aage;
}
}
{code}
5,测试程序
经常看到用@WebServiceRef来实现webservice客户程序,这需要利用服务器提供的工具来产生必要的组件。既然
是SOAP web service,完全可以通过提交SOAP Message的方式来访问,而我们的请求内容(request payload)
将被包裹在所提交的SOAP message中:这也正是我们定义@XmlRootEelement(name="getPersonRequest")的意
义所在,这是通过jaxb进行数据绑定所必须的。
这样,我们可以将创建SOAP Message文件,然后用Java提供的SOAP API建立所需的SOAP Message,并提出请求。
以下是本文提到的GetPerson的测试程序SoapClient。值得注意的是,该测试程序除将返回的相应(response)存
未xml文件外,还利用jaxb api对其进行绑定处理(unmarshalling),直接得到"GetPersonResponse"对象,没
有手工解析(parse)返回的相应。这意味着service客户程序在处理返回的相应结果是有更方便的处理办法,完全
没有必要直接对xml文档进行手工处理。
这是运行SoapClient的终端输出:
Loading request payload: src/t50/service/test/xml/request/getPersonRequest.xml
Saving service response: src/t50/service/test/xml/tmp.xml
web service endpoint url: http://localhost:8080/t50/pojoEcho
Saving service response: src/t50/service/test/xml/response/getPersonResponse.xml
Printing this Person: com.t50.portable.Person: [firstName=john, lastName=wang, age=30001]
SoapClient.java ------------
{code}
package t50.service.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ResourceBundle;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.w3c.dom.Document;
import t50.util.IOUtils;
import t50.util.StringUtils;
import com.t50.portable.GetPersonResponse;
import com.t50.portable.Person;
/**
* A simple SOAP tester client
*
* @author JWang
*
*/
public class SoapClient {
private static final ResourceBundle res = ResourceBundle.getBundle(SoapClient.class.getName());
// "http://localhost:8080"
private static final String host = res.getString("host");
// "/t50/pojoEcho"
private static final String wscontext = res.getString("wscontext");
private static final String wsrequestfile = res.getString("wsrequestfile");
private static final String tmpfile = res.getString("tmpfile");
private static String wsresponsefile = res.getString("wsresponsefile");
public void testWebService() throws Exception {
try {
// construct request soap message
SOAPMessage request = this.createSOAPMsgFromFile(wsrequestfile);
// debug: save request to file
this.saveFile(request, tmpfile);
// construct web service url
String wsurl = host + wscontext;
URL endpoint = new URL(wsurl);
System.out.println("web service endpoint url: " + wsurl);
// send request to web service
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPMessage response = connection.call(request, endpoint);
// save response to file
this.saveFile(response, wsresponsefile);
// try unmarshalling response
SOAPBody body = response.getSOAPBody();
Document doc = body.extractContentAsDocument();
GetPersonResponse resp = (GetPersonResponse)this.unmarshall(doc, GetPersonResponse.class);
this.printPerson(resp.getPerson());
}
catch(Exception e) {
e.printStackTrace();
}
}
// debugging function
private void printPerson(Person person) {
if(person != null) {
StringBuffer sb = new StringBuffer();
sb.append("Printing this Person: ")
.append(Person.class.getName()).append(": [")
.append("firstName=").append(person.getFirstName())
.append(", lastName=").append(person.getLastName())
.append(", age=").append(person.getAge())
.append("]");
System.out.println(sb);
}
}
private Object unmarshall(Document doc, Class clz) throws Exception {
if(doc == null || clz == null) {
return null;
}
// unmarshalls soap message to java object
JAXBContext jc = JAXBContext.newInstance(clz);
Unmarshaller um = jc.createUnmarshaller();
// optional: do schema validation when unmarshalling service response if necessary
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("doc/xsd/t50/person/GetPerson.xsd"));
um.setSchema(schema);
return um.unmarshal(doc);
}
分享到:
相关推荐
2. **JAX-WS**(Java API for XML Web Services)是Java平台上的SOAP(Simple Object Access Protocol)Web服务标准,主要用于创建面向服务的架构(SOA)。JAX-WS提供了处理XML消息、WSDL(Web服务描述语言)和UDDI...
JAX-WS(Java API for XML Web Services)是Java平台标准版(Java SE)和企业版(Java EE)的一部分,它为创建、部署和消费基于SOAP(Simple Object Access Protocol)的Web服务提供了全面的支持。JAX-WS允许开发者...
2. **jaxb-api.jar**:Java Architecture for XML Binding (JAXB) API,用于XML和Java对象之间的转换,是JAX-WS的重要依赖。 3. **cxf-rt-frontend-jaxws.jar** 和 **cxf-rt-transports-http.jar**:Apache CXF是一...
`jaxws-2_0-pfd-spec-api.jar`可能包含了JAX-WS 2.0规范的API类库,供开发者在应用中导入并使用。在实际开发中,还需要相应的实现库,如`metro.jar`,来处理具体的Web服务交互。 **使用场景:** 1. **企业级应用...
WebLogic作为一款强大的Java EE应用服务器,支持JAX-WS标准,但正确配置和部署这些服务需要一些额外的步骤。本指南将详细介绍如何解决这些问题,并提供必要的配置文件和操作步骤。 首先,我们需要了解JAX-WS的工作...
Java实验_JAX-WS主要涉及的是Java平台上的Web服务开发技术,JAX-WS(Java API for XML Web Services)是Java SE和Java EE环境中用于创建和消费SOAP Web服务的标准库。这个实验旨在帮助你深入理解JAX-WS的工作原理...
Java API for XML Web Services (JAX-WS) 是Java平台上的一个标准接口,用于创建和消费Web服务。它是Sun Microsystems在2004年推出的一个重要框架,旨在简化Web服务的开发,使得Java开发者能够更方便地实现基于SOAP...
JAX-WS(Java API for XML Web Services)是Java平台上用于创建Web服务的标准API,版本2.1是其一个重要里程碑。本指南将深入探讨JAX-WS 2.1的核心概念、功能以及如何在实际开发中应用它。以下是对JAX-WS 2.1的详细...
标题中的“一个包含jax-ws和jax-rs的例子(含服务端和客户端)”是指这是一个示例项目,它演示了如何使用Java API for XML Web Services (JAX-WS)和Java API for RESTful Web Services (JAX-RS)来创建和消费Web服务。...
Java API for XML Web Services(JAX-WS)是Java平台上的一个标准,用于创建Web服务和客户端。它提供了一种简单、类型安全的方式来构建和消费基于SOAP的消息传递应用程序,是Java世界中实现Web服务的核心框架之一。...
【标题】"metro-jax-ws-jaxws221x.zip" 提供的是一个关于JAX-WS(Java API for XML Web Services)的开发示例,其中包含了JAX-WS 2.2.1版本的相关组件和库文件。这个压缩包是针对Java开发者设计的,用于帮助他们理解...
【标题】:Web服务之Java API for XML Web Services (JAX-WS) 【内容详解】 JAX-WS,全称为Java API for XML Web Services,是Java平台上的一个标准,用于构建和部署基于SOAP(Simple Object Access Protocol)的...
基于jax-ws 实现的web service client和server端的demo程序。 注:如果使用的是 myeclipse 时 server 部署到tomcat 启动的时候会报错 解决办法:找到myeclipse安装目录下的 plugins 目录里 查找 webservices-rt.jar,...
JAX-WS 2.2 RI 所包含的JAR包集合,包含25个JAR包,列表如下: FastInoset.jar gmbal-api-only.jar ha-api.jar javax.annotation.jar javax.mail_1.4.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jaxws-api...
JAX-WS提供了一种简单的方式来创建SOAP(Simple Object Access Protocol)Web服务,它集成了Java SE和Java EE平台,使得开发者可以方便地实现服务接口和服务实现,然后通过工具自动生成WSDL(Web Service ...
JAX-WS是Java平台标准版(Java SE)和企业版(Java EE)的一部分,用于构建基于SOAP(Simple Object Access Protocol)的Web服务和客户端。这个压缩包可能包含了JAX-WS实现的核心库和其他相关组件。 **描述中的文件...
JAX-WS(Java API for XML Web Services)2.0,由JSR 224定义,是Java EE 5平台的关键组成部分,它是JAX-RPC 1.1的升级版。JAX-WS的主要目标是简化基于XML的Web服务的开发任务,它提供了对SOAP 1.1和1.2,以及XML等...
The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services, particularly SOAP services.... It's a part of the Java SE and Java EE platforms.
Java API for XML Web Services(JAX-WS)是Java平台上的一个标准,用于构建和部署Web服务。它简化了Web服务的开发,使得Java开发者能够更方便地创建、调用和部署SOAP(Simple Object Access Protocol)服务。在这个...
JAX-WS(Java API for XML Web Services)是Java EE 5及更高版本中提供的标准API,用于简化Web服务的开发。 在【MyEclipse中创建WebService的方法文档】中,主要步骤如下: 1. **新建Web Service Project**:首先...