Generating Java artifacts for JAX-WS applications

Use Java™ API for XML-Based Web Services (JAX-WS) tools to generate the necessary JAX-WS and Java Architecture for XML Binding (JAXB) Java artifacts that are needed for JAX-WS web services applications when starting from JavaBeans or enterprise beans components.

Before you begin

To develop a Java API for XML-Based Web Services (JAX-WS) web service application, you must first develop a service endpoint interface (SEI) implementation that explicitly or implicitly describes the SEI.

About this task

When using a bottom-up approach to develop JAX-WS web services, use the wsgen command-line tool on the existing service endpoint implementation. The wsgen tool processes a compiled service endpoint implementation class as input and generates the following portable artifacts:
  • Java Architecture for XML Binding (JAXB) classes that are required to marshal and unmarshal the message contents.
  • a Web Services Description Language (WSDL) file if the optional -wsdl argument is specified.
支援的配置 支援的配置: z/OS® 平台不支援 wsimportwsgenschemagenxjc 指令行工具。 這項功能是由隨執行於 z/OS 平台的 WebSphere® Application Server 提供的組合工具所提供。請閱讀 JAX-WS 應用程式的這些指令行工具以進一步瞭解這些工具。sptcfg
最佳作法 最佳作法: WebSphere Application Server 提供 Java API for XML-Based Web Services (JAX-WS) 和 Java Architecture for XML Binding (JAXB) 工具。 wsimportwsgenschemagenxjc 指令行工具位於 WebSphere Application Server 傳統版app_server_root\bin\ 目錄中。 xjcschemagen 指令位於 Liberty 設定檔的 app_server_root\jaxb\bin\ 目錄中。 在某些情況下,WebSphere Application Server 和 JDK 提供的工具所產生的構件,支援相同層次的規格。一般而言,JDK 工具所產生的構件可以在其他標準執行時期環境之間轉移。 不過,最好的作法是利用這個產品所提供的工具來實現在 WebSphere Application Server 環境內的緊密整合,以及使用可能只在 WebSphere Application Server 環境內受到支援的特性。 如果要利用 JAX-WS 和 JAXB 2.2 版工具,請使用在 app_server_root\bin\ 目錄內之應用程式伺服器所提供的工具。bprac

You are not required to develop a WSDL file when developing JAX-WS Web services using the bottom-up approach of starting with JavaBeans. The use of annotations provides all of the WSDL information necessary to configure the service endpoint or the client. The application server supports WSDL 1.1 documents that comply with Web Services-Interoperability (WS-I) Basic Profile 1.1 specifications and are either Document/Literal style documents or RPC/Literal style documents. Additionally, WSDL documents with bindings that declare a USE attribute of value LITERAL are supported while the value, ENCODED, is not supported. For WSDL documents that implement a Document/Literal wrapped pattern, a root element is declared in the XML schema and is used as an operation wrapper for a message flow. Separate wrapper element definitions exist for both the request and the response.

To ensure the wsgen command does not miss inherited methods on a service endpoint implementation bean, you must either add the @WebService annotation to the desired superclass or you can override the inherited method in the implementation class with a call to the superclass method.

Although a WSDL file is typically optional when developing a JAX-WS service implementation bean, it is required if your JAX-WS endpoints are exposed using the SOAP over JMS transport and you are publishing your WSDL file. If you are developing an enterprise JavaBeans service implementation bean that is invoked using the SOAP over JMS transport, and you want to publish the WSDL so that the published WSDL file contains the fully resolved JMS endpoint URL, then have wsgen tool generate the WSDL file by specifying the -wsdl argument. In this scenario, you must package the WSDL file with your web service application.

In addition to using the tools from the command-line, you can invoke these JAX-WS tools from within the Ant build environments. Use the com.sun.tools.ws.ant.WsGen Ant task from within the Ant build environment to invoke the wsgen tool. To function properly, this Ant task requires that you invoke Ant using the ws_ant script.

Procedure

  1. Locate your service endpoint implementation class file.
  2. Run the wsgen command to generate the portable artifacts. The wsgen tool is located in the app_server_root/bin/ directory.
    [Windows]
    app_server_root\bin\wsgen.bat [options] service_implementation_class
    [AIX][HP-UX][Linux][Solaris]
    app_server_root/bin/ wsgen.sh [options] service_implementation_class
    [IBM i]
    app_server_root/bin/ wsgen [options] service_implementation_class
    (Optional) Use the following options with the wsgen command:
    • Use the -verbose option to see a list of generated files along with additional informational messages.
    • Use the -keep option to keep generated Java files.
    • Use the -wsdl option to generate a WSDL file. If you are developing a service implementation bean that will be invoked using the HTTP transport, then the WSDL file generated by the wsgen command-line tool during this step is optional. However, if you are developing a service implementation bean that will be invoked using the SOAP over JMS transport, then the WSDL file generated by the wsgen tool during this step is required in subsequent developing JAX-WS applications steps, so it is not optional.

    Read about wsgen to learn more about this command and additional options that you can specify.

Results

You have the required Java artifacts to create a JAX-WS web service.

避免困難 避免困難: The wsgen command does not differentiate the XML namespace between multiple XMLType annotations that have the same @XMLType name defined within different Java packages. When this scenario occurs, the following error is produced:
Error: Two classes have the same XML type name ....
Use @XmlType.name and @XmlType.namespace to assign different names to them...
This error indicates that you have classes or @XMLType.name values that have the same name, but exist within different Java packages. To prevent this error, add the @XML.Type.namespace class to the existing @XMLType annotation to differentiate between the XML types.gotcha

With JAX-WS applications, the wsgen command-line tool might not locate shared class files. You can specify the location of these class files using the com.ibm.websphere.webservices.WSDL_Generation_Extra_ClassPath custom property. For more information, see the documentation about the Java virtual machine custom properties.

Example

The following example demonstrates how to use the wsgen command to process the service endpoint implementation class to generate JAX-WS artifacts. This example EchoService service implementation class uses an explicit JavaBeans service endpoint.
  1. Copy the sample EchoServicePortTypeImpl service implementation class file and the associated EchoServicePortType service interface class file into a directory. The directory must contain a directory tree structure that corresponds to the com.ibm.was.wssample.echo package name for the class file.
    /* This is a sample EchoServicePortTypeImpl.java file.    */
    package com.ibm.was.wssample.echo;
    
    
    @javax.jws.WebService(serviceName = "EchoService", endpointInterface = 
    "com.ibm.was.wssample.echo.EchoServicePortType", 
    targetNamespace="http://com/ibm/was/wssample/echo/",
    portName="EchoServicePort")
    
    public class EchoServicePortTypeImpl implements EchoServicePortType {
    
    	public EchoServicePortTypeImpl() {
    	}
    
    	public String invoke(String obj) {
    		System.out.println(">> JAXB Provider Service: 
    Request received.\n");
    		String str = "Failed";
    		if (obj != null) {
    			try {
    				str = obj;
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		return str;
    
    	}
    
    }	
    /* This is a sample EchoServicePortType.java file.    */
    package com.ibm.was.wssample.echo;
    
    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;
    
    
    @WebService(name = "EchoServicePortType", targetNamespace = 
    "http://com/ibm/was/wssample/echo/", 
     wsdlLocation="WEB-INF/wsdl/Echo.wsdl")
    
    public interface EchoServicePortType {
    
    
        /**
         * 
         * @param arg0
         * @return
         *     returns java.lang.String
         */
        @WebMethod
        @WebResult(name = "response", targetNamespace = 
        "http://com/ibm/was/wssample/echo/")
        @RequestWrapper(localName = "invoke", targetNamespace = 
        "http://com/ibm/was/wssample/echo/", 
         className = "com.ibm.was.wssample.echo.Invoke")
        @ResponseWrapper(localName = "echoStringResponse", 
         targetNamespace = "http://com/ibm/was/wssample/echo/", 
         className = "com.ibm.was.wssample.echo.EchoStringResponse")
        public String invoke(
            @WebParam(name = "arg0", targetNamespace = 
            "http://com/ibm/was/wssample/echo/")
            String arg0);
    
    }
  2. Run the wsgen command from the app_server_root\bin\ directory. The -cp option specifies the location of the service implementation class file. The -s option specifies the directory for the generated source files. The -d option specifies the directory for the generated output files. When using the -s or -d options, you must first create the directory for the generated output files.
    [Windows]
    app_server_root\bin\wsgen.bat -wsdl -s c:\generated_source\ -cp c:\my_application\classes\ 
    com.ibm.was.wssample.echo.EchoServicePortTypeImpl -verbose -d c:\generated_artifacts\
    [Linux][AIX][HP-UX][Solaris]Run the wsgen command; for example:
    app_server_root/bin/wsgen.sh -wsdl -s c:/generated_source/ -cp c:/my_application/classes/ 
    com.ibm.was.wssample.echo.EchoServicePortTypeImpl -verbose -d c:/generated_artifacts/
After generating the Java artifacts using the wsgen command, the following files are generated:
/generated_source/com/ibm/was/wssample/echo/EchoStringResponse.java
/generated_source/com/ibm/was/wssample/echo/Invoke.java
/generated_artifacts/EchoService.wsdl
/generated_artifacts/EchoService_schema1.xsd
/generated_artifacts/com/ibm/was/wssample/echo/EchoStringResponse.class
/generated_artifacts/com/ibm/was/wssample/echo/Invoke.class

The EchoStringResponse.java and Invoke.java files are the generated Java class files. The compiled versions of the generated Java files are EchoStringResponse.class and Invoke.class files. The EchoService.wsdl and EchoService_schema1.xsd files are generated because the -wsdl option was specified.

What to do next

Complete the implementation of your JAX-WS web service application.


指出主題類型的圖示 作業主題



時間戳記圖示 前次更新: July 9, 2016 11:17
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_devjaxwsartifactsbean
檔名:twbs_devjaxwsartifactsbean.html