1

I started a enterprise application which having web/mobile, android, ios are the clients. So I decided to use Jersey for RESTFull resources and injecting services with Spring IO. But I am stuck at mainly 2 places.

   1. To Use DI with Spring. -- If I use jersey 2.7, it can able to produce 
    json. Here I can not use Spring DI.
   2. Produce response for complex objects.-- If I use Jerse-Spring 1.8, 
       My Objects not converting as json.
  1. What is the best way to produce json message for complex objects like a Class having collection inside and having persistence/Hibernate annotations.
  2. What is the best solution for DI, is it Spring DI or EJB 3.0 (I am not much aware of EBJ 3.x)

My DB design is over and many of DAO's tested, but to consume my services I am facing a problem. my recent post is: enter link description here

Any help really appreciated.

Community
  • 1
  • 1
Chowdappa
  • 1,580
  • 1
  • 15
  • 31

1 Answers1

0

You can use a HttpServlet with gson lib to create a REST service. See the below simple example


package com.nl.services.ws.sincronizacao;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

@WebServlet(value = "/TestGsonHttp")
public class TestGsonHttp extends HttpServlet{

    @EJB
    private yourEjbClass;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // Your Entity
        Client c =  yourEjbClass.getClientById(1);

        Gson g = new Gson();
        String jsonResult = g.toJson(c);

        PrintWriter writer = resp.getWriter();
        writer.print(jsonResult);;
        writer.flush();
        writer.close();
    }
}

Tiago Zaro
  • 16
  • 1