[JAX-RS] Simple Server and Client using Jersey 1.19 Client API (JSON)

Using Jersey 1.19. in general 1.xx vs 2.xx – the package names differ and some of the Client API methods and ways to get the Client.
Note that the server here is a Dynamic Java Application running on Glassfish 4 so the dependencies are added automatically while the client is a maven project.

1. The server Application:

package com.vvirlan;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("rest")
public class RestApplication extends Application {
	@Override
	public Set<Class<?>> getClasses() {
		Set<Class<?>> classes = new HashSet<>();
		classes.add(ResourceOne.class);
		return classes;
	}
}

2. Server Resource:

package com.vvirlan;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("one")
public class ResourceOne {
	@POST
	@Path("hello")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public MyBean sayJsonHello(@FormParam("x") String x, @FormParam("y") String y) {
		System.out.println("X is " +x+" Y is "+y);
		MyBean bean = new MyBean();
		bean.setName("Bob How "+x);
		bean.setAge(120);
		return bean;
	}
}

3. The bean:

package com.vvirlan;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MyBean {
	private String name;
	private Integer age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}

4. Client (a simple Maven project):

package com.vvirlan;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.representation.Form;
public class JaxRsClientApi {
	public static void main(String[] args) {
		JaxRsClientApi client = new JaxRsClientApi();
		client.run();
	}
	public void run() {
		Client client = Client.create();
		WebResource resource = client.resource("http://localhost:8080/JaxRsSimpleServer/rest/one/hello");
		Form form = new Form();
		form.add("x", "XFOO");
		form.add("y", "YBAR");
		MyBean bean = resource
				.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
				.accept(MediaType.APPLICATION_JSON_TYPE)
				.post(MyBean.class, form);
		System.out.println(bean);
	}
}

5. The bean on the client side (ideally need to share it somehow)

package com.vvirlan;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MyBean {
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "MyBean [name=" + name + ", age=" + age + "]";
	}
}

6. Client pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.vvirlan</groupId>
	<artifactId>jaxrsclientapi</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<jersey.version>1.19</jersey.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			<version>${jersey.version}</version>
		</dependency>

		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-json</artifactId>
			<version>${jersey.version}</version>
		</dependency>
	</dependencies>
</project>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s