Featured Post

jax ws implementation

Follow below steps to write jax-ws service

1)LinuxTest.java
------------------
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seo.testlinux;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author vpilaka
 */
@WebService(name = "TestLINUX", portName = "LinuxPort", serviceName = "LinuxService", targetNamespace = "com.seo.testlinux")
public class LinuxTest {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "execute")
    public String execute(@WebParam(name = "command") String command, @WebParam(name = "param1") String param1, @WebParam(name = "param2") String param2) {
        return command;
    }

}

2) wsgen -keep -d . -cp target/classes com.seo.testlinux.LinuxTest



3) generated classes below

   ExecuteResponse.java
------------------------------

package com.seo.testlinux.jaxws;

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

@XmlRootElement(name = "executeResponse", namespace = "com.seo.testlinux")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "executeResponse", namespace = "com.seo.testlinux")
public class ExecuteResponse {

    @XmlElement(name = "return", namespace = "")
    private String _return;

    /**
     *
     * @return
     *     returns String
     */
    public String getReturn() {
        return this._return;
    }

    /**
     *
     * @param _return
     *     the value for the _return property
     */
    public void setReturn(String _return) {
        this._return = _return;
    }

}

Execute.java
--------------

package com.seo.testlinux.jaxws;

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

@XmlRootElement(name = "execute", namespace = "com.seo.testlinux")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "execute", namespace = "com.seo.testlinux", propOrder = {
    "command",
    "param1",
    "param2"
})
public class Execute {

    @XmlElement(name = "command", namespace = "")
    private String command;
    @XmlElement(name = "param1", namespace = "")
    private String param1;
    @XmlElement(name = "param2", namespace = "")
    private String param2;

    /**
     * 
     * @return
     *     returns String
     */
    public String getCommand() {
        return this.command;
    }

    /**
     * 
     * @param command
     *     the value for the command property
     */
    public void setCommand(String command) {
        this.command = command;
    }

    /**
     * 
     * @return
     *     returns String
     */
    public String getParam1() {
        return this.param1;
    }

    /**
     * 
     * @param param1
     *     the value for the param1 property
     */
    public void setParam1(String param1) {
        this.param1 = param1;
    }

    /**
     * 
     * @return
     *     returns String
     */
    public String getParam2() {
        return this.param2;
    }

    /**
     * 
     * @param param2
     *     the value for the param2 property
     */
    public void setParam2(String param2) {
        this.param2 = param2;
    }

}

4) wsimport -keep -d generated http://localhost:8080/TestLinux/LinuxService?wsdl

5) generated classes below 

    LinuxService.java
  ------------------------


package testlinux.seo.com;

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;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "LinuxService", targetNamespace = "com.seo.testlinux", wsdlLocation = "http://localhost:8080/TestLinux/LinuxService?wsdl")
public class LinuxService
    extends Service
{

    private final static URL LINUXSERVICE_WSDL_LOCATION;
    private final static WebServiceException LINUXSERVICE_EXCEPTION;
    private final static QName LINUXSERVICE_QNAME = new QName("com.seo.testlinux", "LinuxService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:8080/TestLinux/LinuxService?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        LINUXSERVICE_WSDL_LOCATION = url;
        LINUXSERVICE_EXCEPTION = e;
    }

    public LinuxService() {
        super(__getWsdlLocation(), LINUXSERVICE_QNAME);
    }
    public LinuxService(WebServiceFeature... features) {
        super(__getWsdlLocation(), LINUXSERVICE_QNAME, features);
    }

    public LinuxService(URL wsdlLocation) {
        super(wsdlLocation, LINUXSERVICE_QNAME);
    }

    public LinuxService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, LINUXSERVICE_QNAME, features);
    }

    public LinuxService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public LinuxService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return
     *     returns TestLINUX
     */
    @WebEndpoint(name = "LinuxPort")
    public TestLINUX getLinuxPort() {
        return super.getPort(new QName("com.seo.testlinux", "LinuxPort"), TestLINUX.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
     * @return
     *     returns TestLINUX
     */
    @WebEndpoint(name = "LinuxPort")
    public TestLINUX getLinuxPort(WebServiceFeature... features) {
        return super.getPort(new QName("com.seo.testlinux", "LinuxPort"), TestLINUX.class, features);
    }
    private static URL __getWsdlLocation() {
        if (LINUXSERVICE_EXCEPTION!= null) {
            throw LINUXSERVICE_EXCEPTION;
        }
        return LINUXSERVICE_WSDL_LOCATION;
    }

}

TestLinux.java
----------------

package testlinux.seo.com;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebService(name = "TestLINUX", targetNamespace = "com.seo.testlinux")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface TestLINUX {


    /**
     * 
     * @param command
     * @param param1
     * @param param2
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "execute", targetNamespace = "com.seo.testlinux", className = "testlinux.seo.com.Execute")
    @ResponseWrapper(localName = "executeResponse", targetNamespace = "com.seo.testlinux", className = "testlinux.seo.com.ExecuteResponse")
    @Action(input = "com.seo.testlinux/TestLINUX/executeRequest", output = "com.seo.testlinux/TestLINUX/executeResponse")
    public String execute(
        @WebParam(name = "command", targetNamespace = "")
        String command,
        @WebParam(name = "param1", targetNamespace = "")
        String param1,
        @WebParam(name = "param2", targetNamespace = "")
        String param2);

}

6)Webservice client

   MyServiceTest.java
--------------------------
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seo;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.namespace.QName;
import testlinux.seo.com.LinuxService;

/**
 *
 * @author vpilaka
 */
public class MyServiceTest {
    
    public static void main(String[] args) throws MalformedURLException {
        MyService ms=new MyService(new URL("http://localhost:8080/mycalc/TestALL?wsdl"),new QName("com.seo.mycalc","TestALL"));
        Iterator it=ms.getPorts();
        while(it.hasNext()){
            QName qn=it.next();
            System.out.println(qn.getLocalPart()+":"+qn.getNamespaceURI());
        }
        mycalc.seo.com.Test t=(mycalc.seo.com.Test)ms.getPort(new QName("com.seo.mycalc","TestMePort"), mycalc.seo.com.Test.class);
        System.out.println(t.getFile("/Users/vpilaka/"));
        LinuxService ls=new LinuxService(new URL("http://localhost:8080/TestLinux/LinuxService?wsdl"),new QName("com.seo.testlinux","LinuxService"));
        System.out.println(ls.getLinuxPort().execute("ls", "-lrt", ""));
    }
    
}

pom.xml
----------
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.seo</groupId>
    <artifactId>TestLinux</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
<!--                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>-->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>



Comments

Popular posts from this blog

[Inside AdSense] Understanding your eCPM (effective cost per thousand impress...