【问题标题】:Producing and consuming custom JSON Objects in Spring RESTful services在 Spring RESTful 服务中生成和使用自定义 JSON 对象
【发布时间】:2014-11-24 17:48:15
【问题描述】:

我有一些 JSON 对象,它们比我拥有的 java 对象的 JSON 表示形式更复杂。我有构建这些 JSON 对象的方法,我想直接返回并使用它们。我使用 org.json 库来构建我的 JSON。我可以通过将 JSON 对象作为String 返回来使GET 方法工作。这是正确的做法吗?

@RequestMapping(value = "/getjson", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public String getJson() {
    JSONObject json = new JSONObject();
     JSONObject subJson = new JSONObject();
    subJson .put("key", "value");
    json.put("key", subJson);
    return json.toString();
}

现在我想知道如何使用 JSON 对象?作为字符串并将其转换为 JSON 对象?

    @RequestMapping(value = "/post", method = RequestMethod.POST, produces="application/json", consumes="application/json")
    @ResponseBody
    public String post(@RequestBody String json) {
        JSONObject obj = new JSONObject(json);
        //do some things with json, put some header information in json
        return obj.toString();
    }

这是解决我的问题的正确方法吗?我是新手,所以请指出任何可以做得更好的地方。请注意:我不想返回 POJO。

【问题讨论】:

    标签: java json spring rest post


    【解决方案1】:

    我无法使用默认的 Spring 引导 bean 使自动序列化和反序列化工作。最后,在包含 Project Lombok 和 apache BeanUtils 之后,这对我来说效果很好。如果 Spring 不能自动进行序列化,则 Spring 调用 String arg 构造函数。

    @PostMapping("/create")
    @ResponseBody
    public User createUser(HttpServletRequest request, @RequestParam("user") User u) throws IOException {
    
        LOG.info("Got user! " + u);
    
        return u;
    }
    
    
    @ToString() @Getter() @Setter() @NoArgsConstructor()
    public class User {
        private String email;
        private String bio;
        private String image;
        private String displayName;
        private String userId;
        private long lat;
        private long lng;
    
        public User(String json) throws JsonParseException, JsonMappingException, IOException, IllegalAccessException, InvocationTargetException {
            ObjectMapper om = new ObjectMapper();
            User u = om.readValue(json, User.class);
            BeanUtils.copyProperties(this, u);
        }
    }
    

    http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi https://projectlombok.org/

    【讨论】:

      【解决方案2】:

      我认为使用 jackson 库您可以执行以下操作。

      @RequestMapping(value = "/getjson", method = RequestMethod.GET, produces="application/json")
      @ResponseBody
      public String getJson() {
         //your logic
          ObjectMapper mapper = new ObjectMapper();
          return mapper.writeValueAsString(json);
      }
      
      @RequestMapping(value = "/post", method = RequestMethod.POST, produces="application/json", consumes="application/json")
      @ResponseBody
      public String post(@RequestBody String json) {
          POJO pj = new POJO();
          ObjectMapper mapper = new ObjectMapper();
          pj = mapper.readValue(json, POJO.class);
      
          //do some things with json, put some header information in json
          return mapper.writeValueAsString(pj);
      }
      

      【讨论】:

        【解决方案3】:

        我更愿意将 Jackson 与 Spring mvc 一起使用,因为您不必担心对象/json-json/object 的序列化和反序列化。但是如果你还想进程我喜欢用google的gson。

        http://www.javacreed.com/simple-gson-example/

        【讨论】:

          【解决方案4】:

          你可以使用jackson lib,jackson允许使用spring mvc转换成json。

          1. Spring configure @ResponseBody JSON format
          2. Jackson 2.0 with Spring 3.1

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-10-21
            • 1970-01-01
            • 2011-08-08
            • 2016-03-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多