My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
GettingStarted  
Updated Mar 12, 2011 by sundaram...@gmail.com

Any issue,question or suggestion post in JSON Group.

Getting started with JSON Web Service

1) Overview

JSON web service is focused towards developing web service with JSON as input and output format. Also existing web service can be accessed using JSON.
2) Dependency
  1. JAX-WS also called as metro (https://jax-ws.dev.java.net/)

Other default development tools (Ant java and tomcat)

3) Installation

  1. This JSON web service plug-in can be installed by copying the jsonwebservice-ri-.jar into your application's /WEB-INF/lib directory.
2) JAX-WS/Metro installation,
Download jax-ws from https://jax-ws.dev.java.net/ and copy all .jars located from EXTRACT_ROOT/lib into your application's /WEB-INF/lib directory. (optionaly you can ignore -tools.jar,-extra.jar)
Note : installation of Java 1.5 ant and tomcat refer in relevant sites.
4) Creating hello world
  1. Decide your workspace root directory, and create following directory structure.
  2. (For eclipse webtools users dynamic webproject create it for you)

           workspace
              |_helloWorld
              		 |_src
              		 |
                   |_WebContent
                        |_WEB-INF
                        |    |_lib
                        |    |_web.xml
                        |    |_sun-jaxws.xml
                   			|_index.html

2) Now follow up installation procedure described in previous step 3. Its simply copying jar files into your WEB-INF/lib folder.
In case of if your using metro, all jax-ws jars named as webservices-.jar. In case if your using Java webservice developer pack your jar name looks like jax-.jar, saaj.jar, etc
After installation procedure your file structure looks something like,
3) web.xml update.
Now open your helloWorld/WebContent/WEB-INF/web.xml and jax-ws specific following servlet entry.

        <?xml version="1.0" encoding="UTF-8"?>
		<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
		
			<listener>
				<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
			</listener>
		
			<servlet>
				<servlet-name>JAX-WS-Service</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>JAX-WS-Service</servlet-name>
				<url-pattern>/json/*</url-pattern>
			</servlet-mapping>
			
			<!-- OPTIONAL IF YOU ALSO LIKE TO USE SOAP  -->
			<servlet-mapping>
				<servlet-name>JAX-WS-Service</servlet-name>
				<url-pattern>/soap/*</url-pattern>
			</servlet-mapping>
		
		</web-app>
4) sun-jaxws.xml update.
Now open your helloWorld/WebContent/WEB-INF/sun-jaxws.xml and hello world specific implementation entry.
		<?xml version="1.0" encoding="UTF-8"?>
		<endpoints version="2.0" 
			xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" >
		  <endpoint 
		  		name="HelloService" 
		  		implementation="com.mycompany.HelloWorldImpl" 
		  		url-pattern="/json/hello" 
		  		binding="http://jsonplugin.googlecode.com/json/"/>
		  
		   <!-- OPTIONAL IF YOU ALSO LIKE TO USE SOAP  -->
  			
			  <endpoint name="HelloServiceSOAP"
					implementation="com.mycompany.HelloWorldImpl"
					binding="http://schemas.xmlsoap.org/wsdl/soap/http" url-pattern="/soap/hello" />		
		</endpoints>
5) Creating HelloWorldImpl.
Now create folders com/mycompany under src directory. And create HelloWorldImpl.java file. In HelloWorldImpl.java add your methods,
	    
	    package com.mycompany;

			import javax.jws.WebMethod;
			import javax.jws.WebParam;
			import javax.jws.WebResult;
			import javax.jws.WebService;
			import javax.jws.soap.SOAPBinding;
			
			@SOAPBinding(style = SOAPBinding.Style.RPC)
			@WebService (name="HelloService", targetNamespace="http://album.jsonplugin.com/json/")
			public class HelloWorldImpl {
			
				@WebMethod (operationName = "sayHello")
				public @WebResult(name="message") String sayHello(
						@WebParam(name="name") String name){
					return "Hello "+name;
				}
			}
			

NOTE: @SOAPBinding(style = SOAPBinding.Style.RPC) annotation is required, Document binding NOT tested with this JSON codec.
6) Build
To build war creates ant build.xml inside workspace/helloWorld folder.
Following are the content of build.xml

	   	<?xml version="1.0" encoding="UTF-8"?>
			<!-- ====================================================================== 
			     Dec 21, 2010                                                        
			
			     helloWorld    
			     demo
			     ====================================================================== -->
			<project name="helloWorld" default="war">
			  
				<path id ="classpath">
					<fileset dir="./WebContent/WEB-INF/lib" includes="*.jar"/>
					<pathelement location="./classes"/>
				</path>
			
			    <!-- ================================= 
			          target: war              
			         ================================= -->
			    <target name="war" depends="build" description="description">
			    	<war destfile="${ant.project.name}.war" webxml="./WebContent/WEB-INF/web.xml">
			      	<fileset dir="./WebContent/"/>
			      	<classes dir="./classes"/>
			      </war>
			    </target>
			
			    <!-- ================================= 
			          target: build                      
			         =================================  -->
			    <target name="build">
			    	<mkdir dir="classes"/>
			    	<javac classpathref="classpath" srcdir="./src"
			    	    				destdir="./classes" debug="off" source="1.5"/>
			    </target>
			
			</project>
7) Run the ant build.xml . You may down load pre build war from here http://jsonwebservice.googlecode.com/svn/trunk/demos/helloWorld/helloWorld.war
8) Now you can see the helloWorld.war inside workspace/helloWorld.
9) Now copy the way file into tomcat webapps folder or use tomcat manager console to deploy it.
  1. ) After success full deployment look at automated document for json endpoint "http://localhost:8080/helloWorld/json/hello" or look at index page.

  1. ) Once you got doc/test page you're done!!!

Comment by subash...@gmail.com, Feb 25, 2011

hi ,

I am downloaded this application war file.After that i copied in tomcat 1.5 webapp directory.when ever i started server i got .class file bad version message coming.please let me know reason of this. webservices-api.jar file got error message.how can i get new jar. thanks,

subrahmanyam k

Comment by project member sundaram...@gmail.com, Mar 12, 2011

Hi subrahmanyam k,

I belive your using tomcat 5.1 or 5.5. Class files in jar require java 1.5 or greater.
Please check your java version.

Comment by thinhb...@gmail.com, Apr 27, 2012

Hi there,

I followed your guide and create json webservice successfully, and now I want to call this json ws from jQuery library by using getJSON() method, can you give me some piece of code demonstrating this?

Thank you.

Comment by luck...@gmail.com, May 4, 2012

Hi,

Anyone know how to put array or wrapped array parameter on JSON request? I tried but always get null array or wrapped array parameter from the Web Method.

Thanks.

Comment by project member sundaram...@gmail.com, Jul 3, 2012
Comment by project member sundaram...@gmail.com, Jul 3, 2012

If your looking for support or having problem with this codec please post your query in google group http://groups.google.com/group/jsonwebservice

Comment by rocky...@gmail.com, Aug 29, 2012

I could be wrong, but step 3) Installation could be a lot easier if using Maven for dependency injection.

Comment by project member sundaram...@gmail.com, Oct 3, 2012
Comment by urugn.t...@gmail.com, Nov 24, 2014

Hi. i have followed your tutorial very neatly. The problem is that i am deploying in glassfish 4.1 i have included jsonwebservice-rt-0.7 in my web application but i keep getting the below error.

JAXBContextImpl context = (JAXBContextImpl)endPoint.getSEIModel().getJAXBContext();

Severe: caught throwable java.lang.NoSuchMethodError?: com.sun.xml.ws.api.model.SEIModel.getJAXBContext()Lcom/sun/xml/bind/api/JAXBRIContext;

at com.jaxws.json.codec.doc.provider.DefaultEndpointDocument?.process(DefaultEndpointDocument?.java:83) at com.jaxws.json.codec.doc.provider.DefaultEndpointDocument?.doResponse(DefaultEndpointDocument?.java:148) at com.jaxws.json.codec.doc.JSONHttpMetadataPublisher.handleMetadataRequest(JSONHttpMetadataPublisher.java:64) at com.sun.xml.ws.transport.http.HttpAdapter?.handleGet(HttpAdapter?.java:433) at com.sun.xml.ws.transport.http.servlet.ServletAdapter?.invokeAsync(ServletAdapter?.java:193) at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:161) at com.sun.xml.ws.transport.http.servlet.WSServlet.doGet(WSServlet.java:89) at javax.servlet.http.HttpServlet?.service(HttpServlet?.java:687) at javax.servlet.http.HttpServlet?.service(HttpServlet?.java:790) at org.apache.catalina.core.StandardWrapper?.service(StandardWrapper?.java:1682) at org.apache.catalina.core.StandardWrapperValve?.invoke(StandardWrapperValve?.java:318) at org.apache.catalina.core.StandardContextValve?.invoke(StandardContextValve?.java:160) at org.apache.catalina.core.StandardPipeline?.doInvoke(StandardPipeline?.java:734) at org.apache.catalina.core.StandardPipeline?.invoke(StandardPipeline?.java:673) at com.sun.enterprise.web.WebPipeline?.invoke(WebPipeline?.java:99) at org.apache.catalina.core.StandardHostValve?.invoke(StandardHostValve?.java:174) at org.apache.catalina.connector.CoyoteAdapter?.doService(CoyoteAdapter?.java:415) at org.apache.catalina.connector.CoyoteAdapter?.service(CoyoteAdapter?.java:282) at com.sun.enterprise.v3.services.impl.ContainerMapper?$HttpHandlerCallable?.call(ContainerMapper?.java:459) at com.sun.enterprise.v3.services.impl.ContainerMapper?.service(ContainerMapper?.java:167) at org.glassfish.grizzly.http.server.HttpHandler?.runService(HttpHandler?.java:201) at org.glassfish.grizzly.http.server.HttpHandler?.doHandle(HttpHandler?.java:175) at org.glassfish.grizzly.http.server.HttpServerFilter?.handleRead(HttpServerFilter?.java:235) at org.glassfish.grizzly.filterchain.ExecutorResolver?$9.execute(ExecutorResolver?.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain?.executeFilter(DefaultFilterChain?.java:284) at org.glassfish.grizzly.filterchain.DefaultFilterChain?.executeChainPart(DefaultFilterChain?.java:201) at org.glassfish.grizzly.filterchain.DefaultFilterChain?.execute(DefaultFilterChain?.java:133) at org.glassfish.grizzly.filterchain.DefaultFilterChain?.process(DefaultFilterChain?.java:112) at org.glassfish.grizzly.ProcessorExecutor?.execute(ProcessorExecutor?.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable?.run(WorkerThreadIOStrategy.java:137) at org.glassfish.grizzly.threadpool.AbstractThreadPool?$Worker.doWork(AbstractThreadPool?.java:565) at org.glassfish.grizzly.threadpool.AbstractThreadPool?$Worker.run(AbstractThreadPool?.java:545) at java.lang.Thread.run(Thread.java:745)

Also i have tried differrent versions of the jsonwebservice jar file versions 0.5, 0.6, 0.7 and 0.8.0 and i have traced the error to be from line 83 of the

com.jaxws.json.codec.doc.provider.DefaultEndpointDocument?

Kindly assist me in deploying to glassfish 4.1

Powered by Google Project Hosting