首页 > 其他分享 >计算器之webservice实现

计算器之webservice实现

时间:2022-11-29 13:37:14浏览次数:41  
标签:javax webservice 实现 计算器 server new import public String

以下是本人原创,如若转载和使用请注明转载地址。本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!​​​


一、根据我的上一篇博客

​​MyEclipse构建Web Service(Xfire框架)​​

下面是我实现的网页版的计算器,本来想着用php去调用webservice的接口,由于时间原因,现在用java去调用webservice接口。

再根据原先实现过的吧B/S 架构的计算器作业,现将网页说明博客如下:

​​Ext实现简单计算器​​

二、编写自己的类

根据上一篇博客的说明我们建立一个用于实现计算器的webservice类ExpWebService.java,其代码如下所示:

package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.ExpressionFactory;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.JexlHelper;

/**
*
* 项目名称:CalculatorWebService
* 类名称:ExpWebService
* 类描述:
* 创建人:王少帅
* 创建时间:2013-12-12 下午02:58:32
* @version
*/
public class ExpWebService {

/**
*
* expCalculator(使用jexl自带的方式处理exp得到计算器的值)
* @param name
* @return String
* @Exception
*/
public String expCalculator(String exp) throws Exception {
// TODO Auto-generated method stub
Expression e = ExpressionFactory.createExpression(exp);//使用提供的expression提供的方法
JexlContext jc = JexlHelper.createContext();
Object result = e.evaluate(jc);//计算出结果
return result.toString();
}

}

三、客户端去调用相应的webservice,这里是CalculatorWebService

我们先new->webservice client 之后新建项目叫Calculator01,将服务器端的wsdl的url添加好即可,现在我们用到页面去实现计算器的功能,这里我们客户端(其实也在服务器端,以为是B/S 架构的),我们去调用CalculatorWebService提供的方法ExpWebService来求出计算结果。

下面是包的结构:

计算器之webservice实现_2.2.3 WebService学习

之后我们看到我们的action中类CalculatorAction.java中的代码如下:

package com.wss.action;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import server.CalculatorWebServiceClient;
import server.CalculatorWebServicePortType;

/**
*
* 项目名称:Calculator01
* 类名称:CalculatorAction
* 类描述: 接收客户端的计算式来计算结果
* 创建人:王少帅
* 创建时间:2013-12-12 下午03:50:32
* @version
*/
public class CalculatorAction {
public String exp;

public String getExp() {
return exp;
}

public void setExp(String exp) {
this.exp = exp;
}

public void execute() throws Exception{

//这里使用webserviceClient来调用webservice的方法
CalculatorWebServiceClient calculatorWebServiceClient = new CalculatorWebServiceClient();
CalculatorWebServicePortType service = calculatorWebServiceClient.getCalculatorWebServiceHttpPort();

String result = service.expCalculator(exp); //调用expCalculator方法来实现

HttpServletResponse response = ServletActionContext.getResponse();//返回页面进行查看

response.getWriter().write(result);

}

}

四、自动生成的文件分析

如图自动生成的文件:

计算器之webservice实现_apache_02


下面是CalculatorWebServiceClient.java的文件内容:

package server;

import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import javax.xml.namespace.QName;
import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;

public class CalculatorWebServiceClient {

private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
private HashMap endpoints = new HashMap();
private Service service0;

public CalculatorWebServiceClient() {
create0();
Endpoint CalculatorWebServicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://server", "CalculatorWebServicePortTypeLocalEndpoint"), new QName("http://server", "CalculatorWebServicePortTypeLocalBinding"), "xfire.local://CalculatorWebService");
endpoints.put(new QName("http://server", "CalculatorWebServicePortTypeLocalEndpoint"), CalculatorWebServicePortTypeLocalEndpointEP);
Endpoint CalculatorWebServiceHttpPortEP = service0 .addEndpoint(new QName("http://server", "CalculatorWebServiceHttpPort"), new QName("http://server", "CalculatorWebServiceHttpBinding"), "http://localhost:8080/CalculatorWebService/services/CalculatorWebService");
endpoints.put(new QName("http://server", "CalculatorWebServiceHttpPort"), CalculatorWebServiceHttpPortEP);
}

public Object getEndpoint(Endpoint endpoint) {
try {
return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());
} catch (MalformedURLException e) {
throw new XFireRuntimeException("Invalid URL", e);
}
}

public Object getEndpoint(QName name) {
Endpoint endpoint = ((Endpoint) endpoints.get((name)));
if ((endpoint) == null) {
throw new IllegalStateException("No such endpoint!");
}
return getEndpoint((endpoint));
}

public Collection getEndpoints() {
return endpoints.values();
}

private void create0() {
TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
HashMap props = new HashMap();
props.put("annotations.allow.interface", true);
AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));
asf.setBindingCreationEnabled(false);
service0 = asf.create((server.CalculatorWebServicePortType.class), props);
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server", "CalculatorWebServicePortTypeLocalBinding"), "urn:xfire:transport:local");
}
{
AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://server", "CalculatorWebServiceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
}
}

public CalculatorWebServicePortType getCalculatorWebServicePortTypeLocalEndpoint() {
return ((CalculatorWebServicePortType)(this).getEndpoint(new QName("http://server", "CalculatorWebServicePortTypeLocalEndpoint")));
}

public CalculatorWebServicePortType getCalculatorWebServicePortTypeLocalEndpoint(String url) {
CalculatorWebServicePortType var = getCalculatorWebServicePortTypeLocalEndpoint();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}

public CalculatorWebServicePortType getCalculatorWebServiceHttpPort() {
return ((CalculatorWebServicePortType)(this).getEndpoint(new QName("http://server", "CalculatorWebServiceHttpPort")));
}

public CalculatorWebServicePortType getCalculatorWebServiceHttpPort(String url) {
CalculatorWebServicePortType var = getCalculatorWebServiceHttpPort();
org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
return var;
}

public static void main(String[] args) {


CalculatorWebServiceClient client = new CalculatorWebServiceClient();

//create a default service endpoint
CalculatorWebServicePortType service = client.getCalculatorWebServiceHttpPort();

//TODO: Add custom client code here
//
//service.yourServiceOperationHere();

System.out.println("test client completed");
System.exit(0);
}

}


下面这个是CalculatorWebServicePortType.java

package server;

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

@WebService(name = "CalculatorWebServicePortType", targetNamespace = "http://server")
@SOAPBinding(use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public interface CalculatorWebServicePortType {

@WebMethod(operationName = "expCalculator", action = "")
@WebResult(name = "out", targetNamespace = "http://server")
public String expCalculator(
@WebParam(name = "exp", targetNamespace = "http://server") String exp);

}


这个是ExpCalculator.java

package server;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="exp" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"exp"
})
@XmlRootElement(name = "expCalculator")
public class ExpCalculator {

@XmlElement(required = true, nillable = true)
protected String exp;

/**
* Gets the value of the exp property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getExp() {
return exp;
}

/**
* Sets the value of the exp property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setExp(String value) {
this.exp = value;
}

}


这个是ExpCalculatorResponse.java

package server;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="out" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"out"
})
@XmlRootElement(name = "expCalculatorResponse")
public class ExpCalculatorResponse {

@XmlElement(required = true, nillable = true)
protected String out;

/**
* Gets the value of the out property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getOut() {
return out;
}

/**
* Sets the value of the out property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setOut(String value) {
this.out = value;
}

}


这个是ObjectFactory.java

package server;

import javax.xml.bind.annotation.XmlRegistry;


/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the server package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {


/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: server
*
*/
public ObjectFactory() {
}

/**
* Create an instance of {@link ExpCalculator }
*
*/
public ExpCalculator createExpCalculator() {
return new ExpCalculator();
}

/**
* Create an instance of {@link ExpCalculatorResponse }
*
*/
public ExpCalculatorResponse createExpCalculatorResponse() {
return new ExpCalculatorResponse();
}

}


这个是package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://server", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package server;



感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址,并且请尊重劳动成果,谢谢!



作者:少帅




标签:javax,webservice,实现,计算器,server,new,import,public,String
From: https://blog.51cto.com/u_15683012/5894845

相关文章

  • CI分页器pagination的原理及实现
    以下是本人原创,如若转载和使用请注明转载地址。本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!​​下面这段代码是从官网上翻译过来的,介绍了分页的用例pub......
  • NET 6 实现滑动验证码(三)、接口
    题外话,有网友说,这玩意根本很容易破解,确实是这样。但验证码这东西,就跟锁子很类似,防君子不防小人。验证码的发明其实是社会文明的退步。因为它阻碍了真正的使用者,却无法阻挡......
  • 软件离线许可(License)实现原理
    我们经常使用各种开发软件,比如IntelliJIDEA、Navicat、VisualStudio等,这些软件都有一个特点,就是要收费。一般是我们需要去购买一个许可,然后输入这个许可到软件里就能够......
  • 图解Linux进程间通信实现原理(1)
    为Linux应用程序的开发人员,对Linux的进程间通信方式肯定是了如指掌,平时的开发中应该会大量的使用到。当你迅速的在键盘上按下【CTRL+C】终止掉一个正在运行中的命令时,你有没......
  • vue3实现搜索高亮
    vue3实现搜索高亮原文:Vue3文本高亮-掘金(juejin.cn)思路将input的文本进行转义处理(eacapeReg函数),v-html就不能相信用户的一切输入,并且需要匹配**.()*****......
  • OCC BRepPrimAPI_MakeBox实现步骤记录
    OCC构建Box实现过程解析:TopoDS_Shape=BRepPrimAPI_MakeBox(5,5,5); //BRepPrimAPI_MakeBox简单介绍BRepPrimAPI_MakeBox:publicBRepBuilderAPI_MakeShape{p......
  • 第2-4-8章 规则引擎Drools实战(1)-个人所得税计算器
    目录9.Drools实战9.1个人所得税计算器9.1.1名词解释9.1.2计算规则9.1.2.1新税制主要有哪些变化?9.1.2.2资较高人员本次个税较少,可能到年底扣税增加?9.1.2.3关于年度汇......
  • WIN10计算器-关于十六进制
      1.HEX代表十六进制2.DEC代表十进制3.OCT代表八进制4.BIN代表二进制   ......
  • 深度优先遍历和广度优先遍历实现
    深度优先遍历 从根节点开始,沿着树的深度遍历树的节点,尽可能深的搜索树的分支。沿着一条可能的路径一直往下走,深入到不能深入为止。【可以纵向优先搜索】  遍历结果......
  • 基于redis实现秒杀下单
    秒杀下单应该思考的内容:下单时需要判断两点:秒杀是否开始或结束,如果尚未开始或已经结束则无法下单库存是否充足,不足则无法下单下单核心逻辑分析:当用户开始进行下......