Jax-RS如何在SOAP中使用的Restfull Webservices和JAX-WS中使用?

时间:2015-07-11 14:54:34

标签: rest jax-rs jax-ws

Jax-ws(用于XML webservice的Java API)       ----是一组用于以xml格式创建Web服务的API。  jax-rs(java API用于开发人员轻松开发其余Web应用程序)

我试图了解这个api的确切位置。

请帮助我理解这个概念。

1 个答案:

答案 0 :(得分:1)

SOAP和Restfull Webservices只是标准。它们描述了SOAP / Rest Web服务应该如何。例如,SOAP Web服务调用以Envelope开头,并且可以具有(Soap)Header和(Saop)Body。 SOAP服务调用也使用POST http方法作为默认值。有关详情,请查看SOAP specificationRESTful web services

因此,java communutiy也试图遵循这些规范。但是,只需将这些规范复制到java环境中,他们就可以构建自己的api。

让我试着用一个例子解释一下,假设你正在开发一个“Hello,World”服务。您希望使用SOAP作为服务体系结构。完成服务设计后,您可能编写自己的WSDL文档(在SOAP规范中)。在WSDL文档中,您可以使用XML定义对象,还可以使用SOAPAction和操作标记定义服务方法。像这样。

<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://helloworld.bahadirakin.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorldServiceService" targetNamespace="http://helloworld.bahadirakin.com/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://helloworld.bahadirakin.com/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://helloworld.bahadirakin.com/">
  <xs:complexType name="helloRequest">
    <xs:sequence>
      <xs:element minOccurs="0" name="firstName" type="xs:string"/>
      <xs:element minOccurs="0" name="lastName" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="helloRequest" nillable="true" type="helloRequest"/>
  <xs:element name="sayHelloResponse" nillable="true" type="xs:string"/>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sayHello">
    <wsdl:part element="tns:helloRequest" name="helloRequest">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="HelloWorldService">
    <wsdl:operation name="sayHello">
      <wsdl:input message="tns:sayHello" name="sayHello">
    </wsdl:input>
      <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="HelloWorldServiceServiceSoapBinding" type="tns:HelloWorldService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="sayHello" style="document"/>
      <wsdl:input name="sayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="HelloWorldServiceService">
    <wsdl:port binding="tns:HelloWorldServiceServiceSoapBinding" name="HelloWorldServicePort">
      <soap:address location="http://localhost:8080/camel-soap/HelloWorldInternal"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

在此示例中,我们定义了一个服务,其中包含名为sayHello的方法,该方法获取类型helloRequest的参数并返回类型sayHelloResponse。让我们看看我们定义的sayHelloResponse

<xs:element name="sayHelloResponse" nillable="true" type="xs:string"/>

它只是一个使用XML定义的字符串。

但是当您将视角从XML更改为JAVA时,您不会将对象定义为XML,而是使用您的类。此外,您不会使用SOAPAction或任何其他xml标记定义您的方法。你只需在JAVA中编写你的方法。您需要一个框架来为您进行这些更改。否则,您最终将编写框架以使这些更改成为可能。而且,现在您可以使用一些注释将一个简单的POJO定义为SOAP(或Rest)服务。

因此,JAX-WS和JAX-RS只是用于处理SOAP和Restful Service体系结构的java标准(apis)的名称。通过使用这些框架,您永远不会担心如何将Java方法更改为Web服务方法。这是上面给出的WSDL的JAX-WS版本。首先看一下我们的请求参数helloRequest

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "helloRequest", propOrder = {
    "firstName",
    "lastName"
})
public class HelloRequest {

    protected String firstName;
    protected String lastName;
    // GETTERS & SETTERS
}

正如您所看到的,它只是一个POJO。现在让我们来看看我们的服务界面。

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.XmlSeeAlso;

@WebService(targetNamespace = "http://helloworld.bahadirakin.com/", name = "HelloWorldService")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface HelloWorldService {

    @WebResult(name = "sayHelloResponse", targetNamespace = "http://helloworld.bahadirakin.com/", partName = "sayHelloResponse")
    @WebMethod
    public java.lang.String sayHello(
        @WebParam(partName = "helloRequest", name = "helloRequest", targetNamespace = "http://helloworld.bahadirakin.com/")
        HelloRequest helloRequest
    );
}

正如您所看到的,没有信封,没有正文,没有HTTP POST或其他任何内容。您只需编写一个java接口,并使用JAX-WS API将它们与SOAP匹配。是的,您仍然需要对什么是SOAP有基本的了解。但是,您可以通过JAX-WS api轻松开发SOAP Web服务。

由于JAX-WS和JAX-RS只是API,因此每个api都有不同的实现。例如,对于JAX-WS,有metrocxfAxis2实现。对于JAX-RS,有jerseycxf等实现。它们允许相同的API,但实现完全不同。 Applications Server也提供自己的实现。

但是他们仍然需要将java对象转换为XML,因为SOAP规范强制它。还有一些其他库将Java转换为Xml,将XML转换为Java。例如JAXBXStream。此外,当您开发休息服务时,您可能希望将Java对象转换为JSON。有一些工具用于此目的,如JacksonGSon

但是JAX-RS和JAX-WS并不是开发REST或SOAP Web服务的唯一方法。例如,为了开发RESTful Web服务,您可以使用SpringMVC

“那么,我为什么要使用规范”你可能会问。完全取决于您是否使用规范。从理论上讲,如果您使用规范,则可以随时更改实施。但是,由于每个不同的实现提供了不同的酷功能,您可能最终会得到高度耦合的应用程序。所以你可能不会轻易改变它。