Jax-ws( Java API for XML webservice) -是一组用于创建xml格式的ws服务的API。jax-rs (用于开发人员轻松开发rest web应用程序的java API )
我正在尝试了解这个api的确切使用位置。
请帮助我理解这个概念。
发布于 2015-07-12 06:02:25
SOAP和Restfull Webservices只是标准。它们描述了SOAP/Rest web服务应该是怎样的。例如,SOAP web服务调用以信封开始,可以有(Soap)头和(Saop)主体。另外,SOAP服务调用默认使用POST http方法。更多详细信息,请查看SOAP specification和RESTful web services。
因此,java社区也试图遵循这些规范。但他们只是将这些规范复制到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服务体系结构的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将它们与SOAP进行匹配。是的,您仍然需要对什么是SOAP有一个基本的了解。但是多亏了JAX-WS,您可以轻松地开发SOAP web服务。
由于JAX-WS和JAX-RS只是api,所以每个api都有不同的实现。例如,对于JAX-WS,有metro、cxf、Axis2实现。对于JAX-RS,有jersey、cxf等实现。它们允许相同的API,但实现方式完全不同。此外,应用程序服务器还提供了自己的实现。
但他们仍然需要将java对象转换为XML,因为SOAP规范强制这样做。还有其他一些库可以将java转换为Xml以及将XML转换为Java。例如JAXB和XStream。此外,在开发rest服务时,您可能希望将java对象转换为JSON。也有一些工具可以做到这一点,比如Jackson或GSon。
但是JAX-RS和JAX-WS并不是开发REST或SOAP web服务的唯一方法。例如,为了开发RESTful web服务,您可以使用SpringMVC。
“那么,我为什么要使用规范呢?”你可能会问。使用或不使用规范完全由您决定。理论上,如果你使用一个规范,你可以随时改变你的实现。但是,由于每个不同的实现都提供了不同的很酷的特性,所以您最终可能会得到一个高度耦合的应用程序。所以你可能不会那么容易改变。
https://stackoverflow.com/questions/31358432
复制相似问题