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);
}
分享到:
相关推荐
scratch少儿编程逻辑思维游戏源码-Pyorovania.zip
scratch少儿编程逻辑思维游戏源码-弹力猫.zip
scratch少儿编程逻辑思维游戏源码-地心引力.zip
内容概要:本文介绍了一种基于金枪鱼群优化算法(TSO)和支持向量机(SVM)的混合算法模型——TSO-SVM,在多变量时间序列预测中的应用。文中详细解释了TSO-SVM的工作原理,即通过模拟金枪鱼群觅食行为来优化SVM的参数,从而提升预测性能。同时提供了具体的Matlab代码实现步骤,包括参数初始化、模型训练和预测。实验结果显示,TSO-SVM相较于传统SVM方法,显著提升了预测的准确性和稳定性,并展示了良好的泛化能力。 适合人群:对机器学习尤其是时间序列预测感兴趣的科研人员和技术开发者。 使用场景及目标:①需要进行多变量时间序列预测的研究项目;②希望利用自然启发式优化算法改善现有SVM模型效果的技术团队。 其他说明:推荐使用Libsvm工具箱配合Matlab 2018B及以上版本,仅限于Windows 64位操作系统。
内容概要:本文深入探讨了机器视觉技术,重点介绍了OpenCV和Qt在工业相机采集及图像处理中的应用。文中详细讲述了卡尺工具、找线、找圆、颜色检测、模板匹配及形状匹配等关键技术的具体实现方法,并特别强调了海康工业相机采集与基于形状的模板匹配界面的开发。此外,形状匹配算法已被封装成DLL,方便直接调用。通过实际案例和技术解析,帮助读者全面掌握机器视觉系统的构建。 适合人群:对机器视觉感兴趣的初学者、有一定编程基础的研发人员、从事工业自动化领域的工程师。 使用场景及目标:适用于希望深入了解机器视觉技术及其应用场景的专业人士,旨在提升图像处理能力,优化工业自动化流程,提高生产效率。 其他说明:文章不仅提供理论知识,还附有示例代码,便于读者动手实践并加深理解。
scratch少儿编程逻辑思维游戏源码-Billy奇妙之旅.zip
内容概要:本文详细介绍了基于模型开发(MBD)的BMS电池管理系统应用层软件模型。首先概述了BMS的核心任务,即确保电池的安全与高效运行,涉及充电、放电控制、实时监测和电池均衡管理。接着重点讨论了SUMlink电池管理系统策略模型,该模型通过收集和处理电池的数据(如电压、电流、温度),并运用多种算法(如SOC估算、SOH评估)来优化电池性能。文中还阐述了BMC CVS内部通讯协议DBC的作用,确保各模块间数据传输的准确性与效率。最后,介绍了AUTOSAR标准在BMS系统中的应用,特别是针对MPC5644A芯片的底层Build工程,提高了系统的可维护性、可扩展性和可靠性。此外,提到了INCA A2L标定文件的应用,用于配置和调整系统参数,以满足不同需求。 适合人群:从事电动汽车电池管理系统研究与开发的技术人员,尤其是对MBD方法、通信协议和AUTOSAR标准感兴趣的工程师。 使用场景及目标:适用于希望深入了解BMS系统的设计原理和技术细节的专业人士,旨在提高他们对该领域的理解和实际操作能力。 其他说明:通过对代码的具体分析,读者能够更加直观地理解BMS的工作流程及其各个组件间的协作关系。
少儿编程scratch项目源代码文件案例素材-深海困境.zip
少儿编程scratch项目源代码文件案例素材-去吧泡泡糖.zip
KEPServerEX6-6.17.269.0,最新版
scratch少儿编程逻辑思维游戏源码-第二个循环.zip
少儿编程scratch项目源代码文件案例素材-手里剑.zip
少儿编程scratch项目源代码文件案例素材-山地跳跃.zip
内容概要:本文详细介绍了Informed RRT*算法及其在机器人路径规划领域的应用。文章首先解释了该算法相较于传统RRT*算法的优势,即通过将采样范围限制在由起点和终点构成的椭圆区域内来提高搜索效率。接着,文中提供了具体的代码实现,包括椭圆采样的核心公式、路径优化的rewire步骤以及动态调整邻居半径的方法。此外,还讨论了路径队列管理和椭圆区域随路径优化动态更新的重要性。通过这些技术手段,Informed RRT*能够在找到初始路径后显著加快优化速度。 适合人群:对机器人路径规划感兴趣的科研人员、工程师及学生。 使用场景及目标:适用于需要高效路径规划的实际应用场景,如自动驾驶汽车、无人机飞行路径规划等。目标是在复杂环境中快速找到从起点到终点的最佳路径。 其他说明:建议读者在理解理论的基础上,结合提供的代码进行实验,以便更好地掌握算法的工作机制。同时,在不同环境条件下测试算法性能,观察其自适应能力。
内容概要:本文详细介绍了基于COMSOL有限元软件的变压器辐射传热数值分析方法。首先,解释了变压器内外辐射传热的基本机理,包括热量通过传导、对流和辐射的方式传递,重点在于辐射传热的作用及其数学描述。接着,逐步引导读者从零开始构建有限元仿真模型,涵盖模型参数确定、网格划分、材料属性定义、边界条件设置、传热方程设定、仿真运行及结果分析等多个步骤。最后,探讨了进一步研究的方向,如不同因素(温度、材料属性、几何形状)对辐射传热的影响,以及该模型在电力电子设备和热管理系统的潜在应用。 适合人群:电气工程专业学生、初学者和技术爱好者,尤其是对有限元仿真和变压器辐射传热感兴趣的群体。 使用场景及目标:适用于希望通过实际操作掌握有限元仿真技能的人群,旨在帮助他们理解变压器辐射传热机制并能独立完成相关仿真项目。 其他说明:本文不仅提供了理论知识,还附带了详细的视频教程和仿真模型,使学习过程更加直观易懂。
scratch少儿编程逻辑思维游戏源码-Scratch 奔跑.zip
scratch少儿编程逻辑思维游戏源码-超级马里奥兄 回放引擎.zip
少儿编程scratch项目源代码文件案例素材-你准备好了吗?.zip
少儿编程scratch项目源代码文件案例素材-潜水艇.zip
内容概要:本文详细介绍了如何利用Python实现两电平三相并网逆变器的有限控制集模型预测控制(FCS-MPC)。首先建立了逆变器的数学模型,接着探讨了预测控制的核心思想,即通过遍历所有可能的开关状态选择最优解。文中给出了具体的代码实现步骤,包括系统模型建立、开关状态枚举、代价函数设计以及完整的控制循环流程。此外,还讨论了仿真过程中可能出现的问题及其解决方法,如电感参数不准确、延时补偿不足等,并提供了提高控制精度的小技巧。 适合人群:具有一定编程基础并对电力电子控制系统感兴趣的工程师和技术爱好者。 使用场景及目标:适用于希望深入了解FCS-MPC原理并在实践中掌握其实现方法的人群。通过本教程的学习,读者能够独立完成从建模到仿真的全过程,最终获得理想的电流电压波形。 其他说明:文章强调了实际调试中的注意事项,如坐标变换的选择、延时补偿的重要性、代价函数的设计等,这些都是确保控制效果的关键因素。同时指出,在某些情况下,经验性的调整可能会比严格的理论计算带来更好的效果。