我创建了一个企业应用程序,其中包含web / mobile,android,ios是客户端。所以我决定将Jersey用于RESTFull资源并使用Spring IO注入服务。但我主要被困在两个地方。
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.
我的数据库设计已经结束,许多DAO已经过测试,但为了消耗我的服务,我遇到了一个问题。我最近的帖子是:enter link description here
任何帮助真的很感激。
答案 0 :(得分:0)
您可以使用带有gson lib的HttpServlet来创建REST服务。请参阅以下简单示例
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();
}
}