What is difference Javax.jws and javax.xml.ws

2019-04-25 09:26发布

问题:

I am new to Java and trying to jump into WebServices. I found two examples somewhere and I am confused with the available options.

Firstly, javax.jws.WebService with annotation seem to work fine, but there is loads of material on javax.xml.ws. It seems javax.jws is newer and there is not much material available about it.

What is the difference between these two approaches?

回答1:

Web Services Metadata Annotations (JSR 181)

Using annotations from the JSR 181 specification (java.jws.xxx), you can annotate a Web service implementation class or a Web service interface.

e.g. from Deploy JAX-WS Web Services On Tomcat

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{

    @WebMethod String getHelloWorldAsString();

}

JAX-WS 2.0 Annotations (JSR 224)

The JSR 224 specification defines annotations for JAX-WS 2.0 (javax.xml.ws.xxx).

e.g. from Using SOAP Faults and Exceptions in Java JAX-WS

@WebFault(name="CheckVerifyFault",
    targetNamespace="http://www.example.com")
public class CheckVerifyFault extends Exception {

    /**
     * Java type that goes as soapenv:Fault detail element.
     */
    private CheckFaultBean faultInfo;

    public CheckVerifyFault(String message, CheckFaultBean faultInfo) {
        super(message);
        this.faultInfo = faultInfo;
    }

    public CheckVerifyFault(String message, CheckFaultBean faultInfo, 
                               Throwable cause) {
        super(message, cause);
        this.faultInfo = faultInfo;
    }

    public CheckFaultBean getFaultInfo() {
        return faultInfo;
    }
}

Peer Reynders says:

My guess would be that BEA wanted something NOW to put into Weblogic to compete with the equivalent feature in .NET. (see, developing Web services in WebLogic is just "as easy"). Also the annotations specified in JAX-WS 2.0 (JSR-224) seem to provide you with more control. However JSR-224 does explicitly support/include JSR-181 (JSR-224: 7.10 Annotations Defined by JSR-181).

For a more complete discussion about, see JSR 181: a Java Simplification Request

See also:

  • Annotations references
  • JAX-WS annotations reference
  • JSR 181
  • JSR 224


回答2:

These two package namespaces do not define different approaches.

  • If you're creating services based on the Web, there are two options: SOAP services (AKA Web services) or REST services (AKA RESTful services).
  • If implementing SOAP services in Java, the way to go is to use the JAX-WS framework. The framework provides tools, such as wsimport and wsgen, and of course an API.
  • The JAX-WS API includes annotations, classes and interfaces for implementing the code of a SOAP service itself and the code of a service consumer (the client).
  • Altogether these elements of the JAX-WS API use both javax.xml.ws and javax.jws package namespaces.
  • Just follow the tutorials or examples to create the services using JAX-WS. Don't worry about which packages do the API elements come from.
  • But remember to avoid vendor specific API elements. You're more likely to run into these vendor specific elements when using WS-* standards (e.g., WS-Security) beyond WSDL and SOAP.