使用 Java 的 WebServices 计算器示例
Posted
技术标签:
【中文标题】使用 Java 的 WebServices 计算器示例【英文标题】:WebServices Calculator Example using Java 【发布时间】:2012-04-19 00:31:28 【问题描述】:我使用 Jersey 服务使用 Java 中的 Web 服务创建了一个计算器的示例应用程序,用于计算两个数字的总和。
我的代码是: 计算器.java
package p1;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.ws.rs.Path;
@WebService
@Path("/CalculatorService")
public class Calculator
@WebMethod
public String sum(int x,int y)
return ""+(x+y);
计算器.java:
package mypack;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAXWS SI.
* JAX-WS RI 2.0_02-b08-fcs
* Generated source version: 2.0
*
*/
@WebService(name = "Calculator", targetNamespace = "http://p1/")
public interface Calculator
/**
*
* @param arg1
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "sum", targetNamespace = "http://p1/", className = "mypack.Sum")
@ResponseWrapper(localName = "sumResponse", targetNamespace = "http://p1/", className = "mypack.SumResponse")
public String sum(
@WebParam(name = "arg0", targetNamespace = "")
int arg0,
@WebParam(name = "arg1", targetNamespace = "")
int arg1);
CalculatorService.java:
package mypack;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
/**
* This class was generated by the JAXWS SI.
* JAX-WS RI 2.0_02-b08-fcs
* Generated source version: 2.0
*
*/
@WebServiceClient(name = "CalculatorService", targetNamespace = "http://p1/", wsdlLocation = "http://localhost:8081/MyCalculatorWs/CalculatorService?WSDL")
public class CalculatorService
extends Service
private final static URL CALCULATORSERVICE_WSDL_LOCATION;
static
URL url = null;
try
url = new URL("http://localhost:8081/MyCalculatorWs/CalculatorService?WSDL");
catch (MalformedURLException e)
e.printStackTrace();
CALCULATORSERVICE_WSDL_LOCATION = url;
public CalculatorService(URL wsdlLocation, QName serviceName)
super(wsdlLocation, serviceName);
public CalculatorService()
super(CALCULATORSERVICE_WSDL_LOCATION, new QName("http://p1/", "CalculatorService"));
/**
*
* @return
* returns Calculator
*/
@WebEndpoint(name = "CalculatorPort")
public Calculator getCalculatorPort()
return (Calculator)super.getPort(new QName("http://p1/", "CalculatorPort"), Calculator.class);
ObjectFactory.java:
package mypack;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the mypack 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
private final static QName _Sum_QNAME = new QName("http://p1/", "sum");
private final static QName _SumResponse_QNAME = new QName("http://p1/", "sumResponse");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: mypack
*
*/
public ObjectFactory()
/**
* Create an instance of @link Sum
*
*/
public Sum createSum()
return new Sum();
/**
* Create an instance of @link SumResponse
*
*/
public SumResponse createSumResponse()
return new SumResponse();
/**
* Create an instance of @link JAXBElement @code <@link Sum @code >
*
*/
@XmlElementDecl(namespace = "http://p1/", name = "sum")
public JAXBElement<Sum> createSum(Sum value)
return new JAXBElement<Sum>(_Sum_QNAME, Sum.class, null, value);
/**
* Create an instance of @link JAXBElement @code <@link SumResponse @code >
*
*/
@XmlElementDecl(namespace = "http://p1/", name = "sumResponse")
public JAXBElement<SumResponse> createSumResponse(SumResponse value)
return new JAXBElement<SumResponse>(_SumResponse_QNAME, SumResponse.class, null, value);
包信息.java:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://p1/")
package mypack;
Sum.java:
package mypack;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for sum complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="sum">
* <complexContent>
* <restriction base="http://www.w3.org/2001/XMLSchemaanyType">
* <sequence>
* <element name="arg0" type="http://www.w3.org/2001/XMLSchemaint"/>
* <element name="arg1" type="http://www.w3.org/2001/XMLSchemaint"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sum", propOrder =
"arg0",
"arg1"
)
public class Sum
protected int arg0;
protected int arg1;
/**
* Gets the value of the arg0 property.
*
*/
public int getArg0()
return arg0;
/**
* Sets the value of the arg0 property.
*
*/
public void setArg0(int value)
this.arg0 = value;
/**
* Gets the value of the arg1 property.
*
*/
public int getArg1()
return arg1;
/**
* Sets the value of the arg1 property.
*
*/
public void setArg1(int value)
this.arg1 = value;
SumResponse.java:
package mypack;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for sumResponse complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="sumResponse">
* <complexContent>
* <restriction base="http://www.w3.org/2001/XMLSchemaanyType">
* <sequence>
* <element name="return" type="http://www.w3.org/2001/XMLSchemastring" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sumResponse", propOrder =
"_return"
)
public class SumResponse
@XmlElement(name = "return")
protected String _return;
/**
* Gets the value of the return property.
*
* @return
* possible object is
* @link String
*
*/
public String getReturn()
return _return;
/**
* Sets the value of the return property.
*
* @param value
* allowed object is
* @link String
*
*/
public void setReturn(String value)
this._return = value;
而我的 web.xml 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>CalculatorEx</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.restful.demo.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我正在使用以下方式执行我的应用程序:
http://localhost:8080/CalculatorEx/rest/CalculatorService
我收到这样的错误:
exception:
javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:679)
root cause:
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1298)
com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:169)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:771)
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:679)
【问题讨论】:
是不是因为您的 web.xml 引用了“com.restful.demo.resources”,但您的代码在包“mypack”中? 在 web.xml 中添加包“mypack”后出现同样的错误 我正在使用这个例子:jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey 请给出建议如何做。 【参考方案1】:因为:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.restful.demo.resources</param-value>
</init-param>
改成:
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>p1</param-value>
</init-param>
【讨论】:
在web.xml中添加包后出现错误:请求的资源不允许指定的HTTP方法(方法不允许)。 @Praveensai 您如何调用网络服务?你是如何为 sum 传递参数的? 为此,我们使用 xml 并使用注释:@XmlAccessorType(XmlAccessType.FIELD) 为 sum 传递参数:@XmlType(name = "sum", propOrder = "arg0", " sum 类中的 arg1") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sumResponse", propOrder = SumResponse 类中的 "_return" 请在 public String sum(int x,int y) 之前添加@GET/@POST 以指定您想要的网络方法请求类型。并使用 GET/POST 相应地调用它。这将解决上述错误。一切顺利。 将@POST 添加到方法后,它给出了相同的错误【参考方案2】:我看到的是使用 JAX-WS Web 服务和注册 Jersey servlet 的 web.xml 的代码。您的代码中没有一个 JAX-RS 资源。所以这就是你收到错误的原因。查看 JAX-RS 规范,了解如何编写 JAX-RS 资源。
【讨论】:
我是 webservice 的新手。请帮助我 我正在使用这个示例:jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey 请提供建议。 该示例看起来不错,但您的代码非常不同。好像您用 jax-ws 替换了所有 jax-rs 注释。所以难怪它不起作用。只需阅读有关 jax-rs 的任何介绍性文章。例如。 jersey.java.net/nonav/documentation/latest/jax-rs.html 该示例工作正常,但没有给出 xml 中的所有值 “不给出所有值”是什么意思?结果要么是 XML,要么不是。不能介于两者之间。也看看documentiation。以上是关于使用 Java 的 WebServices 计算器示例的主要内容,如果未能解决你的问题,请参考以下文章
Webservices - Axis2 ClientStub 策略错误
java webservices 以Axis1.4方式 调用sap webservice接口.
C# 接收 java webservices接口返回的Byte[] 数组时,大于90M报错System.OutOfMemoryException 求高手解答