Tech Rocks

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more...

Thursday, June 2, 2016

JSON Object with JAX RS

MessageRestService.java
package com.jeetu.rest;

import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.LinkedHashMap;
import java.util.Map;

@Path("/message")
public class MessageRestService {

    @POST
    @Path("/sendParams")
    @Consumes("application/json")
    public Response consumeJSONParams( Map student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendParams
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10", "params":{"a": 1, "b": 2}}
        Content-Type = application/json
        */

        LinkedHashMap params = null;
        String id = "";

        id = (String) student.get("id");
        params = (LinkedHashMap) student.get("params");

        return Response.status(200).entity(params.toString()).build();
    }

    @POST
    @Path("/sendMapper")
    @Consumes("application/json")
    public Response consumeJSONMapper( String student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendMapper
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10", "params":{"a": 1, "b": 2}}
        Content-Type = application/json
        */

        JSONObject output = null;
        JSONObject params = null;
        String id = "";

        try {
            output = new JSONObject(student);
            id = (String) output.get("id");
            params = output.getJSONObject("params");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //return Response.status(200).entity(output.toString()).build();
        //return Response.status(200).entity(id).build();
        return Response.status(200).entity(params.toString()).build();
    }

    @POST
    @Path("/sendRequest")
    @Consumes("application/json")
    public Response consumeJSONRequest( String student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendRequest
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10"}
        Content-Type = application/json
        */

        Gson gson = new Gson();
        Student output = gson.fromJson(student, Student.class);

        return Response.status(200).entity(gson.toJson(output)).build();
    }

    @POST
    @Path("/sendString")
    @Consumes("application/json")
    public Response consumeJSONString( String student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendString
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10"}
        Content-Type = application/json
        */

        Gson gson = new Gson();
        Student output = gson.fromJson(student, Student.class);

        return Response.status(200).entity(output).build();
    }


    @POST
    @Path("/send")
    @Consumes("application/json")
    public Response consumeJSON( Student student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/send
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10"}
        Content-Type = application/json
        */

        String output = student.toString();

        return Response.status(200).entity(output).build();
    }

    @GET
    @Path("/print/{name}")
    @Produces("application/json")
    public Student produceJSON( @PathParam("name") String name ) {

        /*
        http://jbosseap.jeetualex.info/rest/message/print/James
        */

        Student st = new Student(name, "Marco",19,12);

        return st;

    }



    @GET
    @Path("/{param}")
    public Response printMessage(@PathParam("param") String msg) {

        /*
        http://jbosseap.jeetualex.info/rest/message/jeetu
        */

        String result = "Restful example : " + msg;

        return Response.status(200).entity(result).build();

    }

}
Student.java
package com.jeetu.rest;

public class Student {

    private int id;
    private String firstName;
    private String lastName;
    private int age;

    // Must have no-argument constructor
    public Student() {

    }

    public Student(String fname, String lname, int age, int id) {
        this.firstName = fname;
        this.lastName = lname;
        this.age = age;
        this.id = id;
    }

    public void setFirstName(String fname) {
        this.firstName = fname;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setLastName(String lname) {
        this.lastName = lname;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    @Override
    public String toString() {
        return new StringBuffer(" First Name : ").append(this.firstName)
                .append(" Last Name : ").append(this.lastName)
                .append(" Age : ").append(this.age).append(" ID : ")
                .append(this.id).toString();
    }

}
pom.xml
dependency\>
                groupid>org.jboss.resteasy/groupid>
                artifactid>resteasy-jaxrs/artifactid>
                version>2.2.1.GA/version>
            /dependency>

            dependency>
                groupid>org.jboss.resteasy/groupid>
                artifactid>resteasy-jackson-provider/artifactid>
                version>3.0.4.Final/version>
            /dependency>
Web.xml
!--rest easy !!!-->
    !-- Auto scan REST service -->
    context-param>
        param-name>resteasy.scan/param-name>
        param-value>true/param-value>
    /context-param>

    !-- this need same with resteasy servlet url-pattern -->
    context-param>
        param-name>resteasy.servlet.mapping.prefix/param-name>
        param-value>/rest/param-value>
    /context-param>

    listener>
        listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        /listener-class>
    /listener>

    servlet>
        servlet-name>resteasy-servlet
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>
    !--rest easy !!!-->


 Reference link 1, link 2, link 3

2 comments :

caris said...

Ace4sure is the website that deals in preparation material for the exam for many years. According to my exposure and research, this is the right platform where you can get exact 70-767 Dumps.

Big B said...

I was very pleased to find this web-site. I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.
black friday hot tub deals

Post a Comment