0% found this document useful (0 votes)
81 views6 pages

EesyEasy Web Service and Client With Jackson Binding

1. The document describes how to create a RESTful web service using Java with RESTEasy and Jackson for JSON binding. 2. It includes instructions for configuring a Maven project with RESTEasy dependencies, defining a REST endpoint using JAX-RS annotations, and creating a client to call the REST service. 3. The REST service exposes methods to get a JSON representation of a Person object by name, and to create a new Person by posting JSON and returning the response.

Uploaded by

Kau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views6 pages

EesyEasy Web Service and Client With Jackson Binding

1. The document describes how to create a RESTful web service using Java with RESTEasy and Jackson for JSON binding. 2. It includes instructions for configuring a Maven project with RESTEasy dependencies, defining a REST endpoint using JAX-RS annotations, and creating a client to call the REST service. 3. The REST service exposes methods to get a JSON representation of a Person object by name, and to create a new Person by posting JSON and returning the response.

Uploaded by

Kau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EesyEasy Web Service and client with Jackson Binding.

1. Create a Maven Web application


2. web.xml file configurations
<web-app id="WebApp_ID" 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">
<display-name>Restful Web Application</display-name>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.testrest.rest.MessageRestService</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

pom.xml file configuration changers


Dependencies add to pom.xml
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.4.Final</version>
</dependency>
Repositories add to pom.xml
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>

RestEasy Web Service End Point.


java bean class
package com.testrest.bean;
public class Person {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return new StringBuffer(" name : ").append(this.name)
.append(" Age : ").append(this.age).toString();
}
}
Web Service Application class
package com.testrest.app;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import com.testrest.rest.MessageRestService;
public class MessageApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public MessageApplication() {
singletons.add(new MessageRestService());
}

@Override
public Set<Object> getSingletons() {
return singletons;
}

Web Service class


package com.testrest.rest;
import
import
import
import
import
import
import
import

javax.ws.rs.*;
javax.ws.rs.Consumes;
javax.ws.rs.GET;
javax.ws.rs.POST;
javax.ws.rs.Path;
javax.ws.rs.PathParam;
javax.ws.rs.Produces;
javax.ws.rs.core.Response;

import com.testrest.bean.Person;
@Path("/message")
public class MessageRestService {
@GET
@Path("/persons/{name}")
@Produces("application/json")
public Person printMessage(@PathParam("name") String name) {
Person person = new Person();
person.setAge(1);
person.setName(name);
return person;
}
@POST
@Path("/createPerson")
@Consumes("application/json")
@Produces("application/json")
public Response getPerson(Person person) {
person.setAge(person.getAge()+ 1);
person.setName(person.getName()+" Test");
return Response.status(200).entity(person.toString()).build();
}
}

Web Service Client


ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target =
client.target("http://localhost:8080/RESTfulExample/rest/message/student");
Response response = target.request().post(Entity.entity(st,
"application/json"));
response.readEntity(String.class

You might also like