【问题标题】:Produce JSON message for complex object为复杂对象生成 JSON 消息
【发布时间】:2016-07-14 16:48:06
【问题描述】:

我启动了一个企业应用程序,其客户端为 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.
  1. 为复杂对象生成 json 消息的最佳方法是什么,例如内部具有集合并具有持久性/Hibernate 注释的类。
  2. 什么是 DI 的最佳解决方案,是 Spring DI 还是 EJB 3.0(我不太了解 EBJ 3.x)

我的数据库设计已经结束,许多 DAO 都经过了测试,但是要使用我的服务,我遇到了问题。我最近的帖子是:enter link description here

非常感谢任何帮助。

【问题讨论】:

    标签: java json spring rest jersey


    【解决方案1】:

    您可以使用带有 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(); } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多