Sun Glassfish Enterprise Server |
package enterprise.hello_webservice;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import javax.xml.ws.soap.Addressing;
import javax.xml.ws.WebServiceException;
@MTOM
@WebService
public class Hello
{
public String getHello(String name)
{
return "Hello " + name + "!";
}
public byte[] getHelloBinary(byte[] name) {
try {
String nameStr = new String(name,"UTF-8");
return ("Hello " + nameStr + "!").getBytes("UTF-8");
} catch(java.io.UnsupportedEncodingException ex) {
throw new WebServiceException(ex);
}
}
}
This sample WebServcie is configured with MTOM feature through @MTOM. See Metro guide for more details on MTOM feature
You can verify the Webservice is active by entering this in a browser (example server host and port):
http://localhost:8080/hello-webserviceref/HelloService?wsdl
${javaee.home}/bin/wsimport -Xendorsed -package=
enterprise.hello_webserviceref_client
-d ./build
http://${javaee.server.name}:${javaee.server.port}/
hello-webserviceref
/HelloService?wsdl
Note: the Xendorsed option since JDK 6 u4 JAXWS 2.1 is
packaged as part of rt.jar. We want to use JAXWS 2.2 apis and need to
override the JAXWS 2.1 which is part of JDK 6u4 so we need to use
Xendorsed"
import javax.xml.ws.WebServiceRef;
import javax.xml.ws.soap.MTOM;
import enterprise.hello_webserviceref_client.HelloService;
import enterprise.hello_webserviceref_client.Hello;
public class WebServiceRef_AppClient {
@MTOM
@javax.xml.ws.WebServiceRef(HelloService.class)
static Hello port;
public static void main(String[] args) {
WebServiceRef_AppClient client = new WebServiceRef_AppClient();
client.doHelloBinary();
}
public void doHelloBinary() {
try {
//HelloService service = new HelloService();
//Hello port = service.getHelloPort();
byte[] ret =
port.getHelloBinary(System.getProperty("user.name").getBytes("UTF-8"));
System.out.println("Hello result = " + new String(ret,"UTF-8"));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Note: the use of @WebServiceRef and configuring MTOMFeature on the WebService client, which will be injected by the JavaEE container.
The client will print out a greeting from the Webservice using your
name.
Perform the following steps to build, deploy, and run the
application:
app_dir
is the sample application base
directory: samples_install_dir/javaee6/
webservices/hello-jaxws2.2
.
Change directory to app_dir.
all
target.
app_dir> ant
all
clean
to undeploy the sample
application and to
remove the temporary directories such as build and dist.
app_dir> ant
clean
Perform the following steps to build, deploy, and run the application using NetBeans IDE:
samples_install_dir/javaee6/webservices/
directory, select hello-webserviceref
, and click Open Project.hello-webserviceref
and select Run to build, deploy, and run the project.If you have problems when running the application, refer the troubleshooting document.
Copyright © 2009 Sun Microsystems, Inc. All rights reserved.