Skip to content

Instantly share code, notes, and snippets.

@ifsantos
Last active December 11, 2015 14:18
Show Gist options
  • Save ifsantos/4612994 to your computer and use it in GitHub Desktop.
Save ifsantos/4612994 to your computer and use it in GitHub Desktop.
Webservice example, using only JAX-WS (without CXF). Comments describe what else to do in the deploy conguration.
package com.hcllearning.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/*
* Use wsgen to generate web service artifacts
* ===========================================
Start a command prompt and navigate to the root of the project directory. Make sure the JDK is in your PATH,
or else you can't execute the wsgen command. Create a wsdl directory in WebContent/WEB-INF/ or else wsgen will complain.
Use the wsgen command to generate the artifacts like this:
wsgen -classpath build/classes/ -wsdl
-r WebContent/WEB-INF/wsdl -s src
-d build/classes/ com.hcllearning.webservice.BasicService
This command will generate the source, class files, and WSDL file for the web service implemented as
com.javaeenotes.ExampleWS. When wsgen complains about not finding the class, you have to build the project first.
When the command is successful, you can refresh the project and you'll find the artifacts in your project tree.
The generated WSDL-file needs some editting, before you can use it. Open the WSDL-file and search for
REPLACE_WITH_ACTUAL_URL. Replace this string with the web service URL: http://127.0.0.1:8080/webservice/ExampleWSService,
and save the file.
Add the following to web.xml
============================
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>basicService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>basicService</servlet-name>
<url-pattern>/services/BasicService</url-pattern>
</servlet-mapping>
Create a file sun-jaxws.xml aside web.xml:
==========================================
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="BasicService"
implementation="com.hcllearning.webservice.BasicService"
url-pattern="/services/BasicService"/>
</endpoints>
Add those files to the classpath (Jax-WS - http://jax-ws.java.net/):
=====================================================================
gmbal-api-only.jar
ha-api.jar
jaxb-impl.jar
jaxws-api.jar
jaxws-rt.jar
management-api.jar
policy.jar
stax-ex.jar
streambuffer.jar
*/
@WebService(portName = "BasicServicePort",
serviceName = "BasicService",
targetNamespace = "http://hcllearning.com/jaxws-project")
public class BasicService {
@WebMethod
public @WebResult(name="greeting")String sayHello(@WebParam(name="name") String name){
System.out.println("Chamando sayHello");
return "O webservice diz hello a voce, "+name;
}
@WebMethod
public @WebResult(name="result")int sum(@WebParam(name="term1") int term1, @WebParam(name="term2") int term2){
System.out.println("Chamando sum");
return term1 + term2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment