Obs: !!!!!!!!!If you use jre/jdk 6 download the jboss version compiled with jdk 6. Otherwise you will get weird errors when you try to access the webservice.
I assume that you downloaded the correct version of jboss on your computer. Java EE/jax-ws makes webservices creation a formality.
@WebService
@SOAPBinding(style=Style.RPC)@Stateless
public interface ServiciuTest {
@WebMethod
public String sayHello(@WebParameter(name="message")String msg);
}
@Stateless
@WebService(endpointInterface="ServiciuTest")
public class ServiciuTestBean {
public String sayHello(String msg) {
return msg;
}
}
This is all you have to do for creating a functional web service. In this moment the webservice can be deployed on a container that supports jax-ws. In Java EE, the above mentioned class will be exposed as a web service and as a session bean. The interface described is an endpoint(spec) for the web service. The annotation used in the method declaration alter the way the wsdl file is generated. You must keep in mind that you won't be able to return classes which aren't serializable.
In the following paragraphs I present how a complex web service can be implemented. This web service downloads a file from server.
@WebService
@SOAPBinding(style=Style.RPC)@Stateless
public interface ServiciuFilesTest {
@WebMethod
public byte[] getFile(@WebParam(name="file_name")String fName);
}
@Statelesspublic byte[] getFile(String fName) {
@WebService(endpointInterface="ServiciuFilesTest")
public class ServiciuFilesTestBean {
byte[] deRet = new byte[(int)(new File(fName)).length()];return deRet;
try {
FileInputStream file = new FileInputStream(fName);
file.read(deRet);
file.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
return null;
}
}
}
This is all you have to do. In this manner you can send files to a client through the web service. Usually, it is recommended that you encode the content sent using Base64.
Niciun comentariu:
Trimiteți un comentariu