【问题标题】:ResponseBody in Spring MVC 2Spring MVC 2 中的响应体
【发布时间】:2014-02-21 09:05:15
【问题描述】:

我目前正致力于在现有系统中实现 REST Web 服务。该系统在版本 2(特别是 2.5.6.SEC02)中使用 Spring。我无法将它升级到版本 3,因为它可能会破坏现有的系统组件。这个系统的其余部分我们没有做,没有源代码也不想失去保修,所以 Spring 版本应该基本保持原样:)

问题是,如何实现带有来自/到 JSON 的自动 DTO 序列化的 Rest WS?我在类路径上有适当的杰克逊库。 Spring 2 似乎还不知道 @RequestBody 和 @ResponseBody 。有没有其他可以使用的注解,或者其他方式?

【问题讨论】:

    标签: java json spring rest spring-mvc


    【解决方案1】:

    您可能需要手动解析 JSON 字符串并将其写入响应中才能为您工作。我建议使用 jackson2 API。

    https://github.com/FasterXML/jackson

    首先,从请求中接受一个 json 字符串作为参数,然后使用 jackson ObjectMapper 手动解析与 POJO 之间的字符串。

    这里是 jQuery/JavaScript:

    function incrementAge(){
    
        var person = {name:"Hubert",age:32};
    
        $.ajax({
    
            dataType: "json",
            url: "/myapp/MyAction",
            type: "POST",
            data: {
                person: JSON.stringify(person)
            }
    
        })
        .done(function (response, textStatus, jqXHR) {
    
            alert(response.name);//Hubert
            alert(response.age);//33
            //Do stuff here
        });
    }
    

    人pojo:

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    
    //Allows name or age to be null or empty, which I like to do to make things easier on the JavaScript side
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Person{
    
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    这是控制器:

    import com.fasterxml.jackson.databind.ObjectMapper;
    
    /*snip*/
    
    @Controller
    public class MyController{
    
        //I prefer to have a single instance of the mapper and then inject it using Spring autowiring
        private ObjectMapper mapper;
    
        @Autowired
        public MyController(ObjectMapper objectMapper){
    
            this.objectMapper = objectMapper;
        }
    
        @RequestMapping(value="/myapp/MyAction", method= {RequestMethod.POST})
        public void myAction(@RequestParam(value = "person") String json, 
                             HttpServletResponse response) throws IOException {
    
            Person pojo = objectMapper.readValue(new StringReader(json), Person.class);
    
            int age = pojo.getAge();
            age++;
            pojo.setAge(age);
    
            objectMapper.writeValue(response.getOutputStream(),pojo);
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用 Spring MVC 控制器和来自 json.org 的 JSONObject 的 DIY 方法。只需使用它对返回的对象进行 json-serialize 并使用适当的标头将其刷新到响应中。

      它有其缺陷(我建议您在尝试发送集合时使用带有 getter 的简单包装类),但多年来我一直对此感到满意。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-20
        • 2017-07-27
        相关资源
        最近更新 更多