Tuesday, October 4, 2011

create a web service in java without server

Sometimes when create a Web service in java dont return data but there are not any error. This happenig because the classs interface and the implementation dont have the same annotations. Solution: copy exactily the same funtion interface.


Monday, October 3, 2011

Create a Web Service in java without tomcat or glassfish

It`s posible create a Web service without a server. With java JSE 6 it's easy.

Create a interface with a method
package mycountry.entreprise.myservice;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name = "MyService",
        targetNamespace = "http://MyService.entreprise.country/",
        serviceName = "MyMethod")
public interface MyService {
   
    @WebResult(name="returnValue")
    public abstract String[] oneMethod(
            @WebParam(name="param1") String param1);

}


Create a implementation of interface


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

import javax.xml.ws.Endpoint;

@WebService(name = "MyService",
        targetNamespace = "http://MyService.entreprise.country/",
        serviceName = "MyServiceService")
public class MyServiceImpl implements MyService{
   public String[] oneMethod(String param1) {
String []data=  {"1","AA","1","01};
return data;
}

 @WebMethod(exclude = true)
    public static void main(String[] args) {

          Endpoint.publish(
             "http://localhost:8080/service/myservice",
             new MyServiceImpl ());

    }

}


and create a client for the web service

import javax.xml.namespace.QName;
import javax.xml.ws.Service;


public class Client {
  
    static QName serviceQName = new QName("http://MyService.entreprise.country/","MyServiceService");
    static Service myServiceService;
   
    public static void main(String[] args) {
      try {
          URL wsdlLocation= new URL("http://localhost:8080/service/myservice?WSDL");
          myServiceService=Service.create(wsdlLocation, serviceQName);
         
          Cliente client = new Cliente();
          client.doTest(args);
       
      } catch(Exception e) {
         
        e.printStackTrace();
      }
   }
  
   public void doTest(String[] args) {
       System.out.println(    " Data recieve ... " + myServiceService);
      
       MyService myService=myServiceService.getPort(MyService.class);
      
       String[] listofdatas=  myService.oneMethod("send datas");

       System.out.println(" incoming data .. :"+ listofdatas.length);
   }
}


it's to easy...