简体   繁体   English

Spring MVC 2中的ResponseBody

[英]ResponseBody in Spring MVC 2

I am currently working on implementing REST webservices into existing system. 我目前正在将REST Web服务实现到现有系统中。 This system is using Spring in version 2 (particularly 2.5.6.SEC02). 该系统在版本2中使用Spring(特别是2.5.6.SEC02)。 I can't upgrade it to version 3 as it could break existing system components. 我无法将其升级到版本3,因为它可能会破坏现有的系统组件。 We didn't make the rest of this system, don't have source codes and don't want to lose warranty, so Spring version should basically stay as it is :) 我们没有制作本系统的其余部分,没有源代码而且不想失去保修,所以Spring版本应该基本保持原样:)

The question is, how can I implement Rest WS with automatic DTO serialization from/to JSON? 问题是,我如何使用自动DTO序列化实现Rest WS到JSON? I have appropriate Jackson libraries on the classpath. 我在类路径上有适当的杰克逊库。 Spring 2 doesn't seem to know about @RequestBody and @ResponseBody yet. Spring 2似乎还不了解@RequestBody和@ResponseBody。 Are there any other annotations that can be used, or some alternative way? 是否有其他可以使用的注释,或者某种替代方式?

You may need to manually parse the JSON String and write it to the response for this to work for you. 您可能需要手动解析JSON字符串并将其写入响应,以便为您工作。 I suggest using the jackson2 API. 我建议使用jackson2 API。

https://github.com/FasterXML/jackson https://github.com/FasterXML/jackson

First, accept a json String as the argument from the request, and then manually parse the String to and from a POJO using the jackson ObjectMapper. 首先,接受一个json String作为请求中的参数,然后使用jackson ObjectMapper手动将字符串解析为POJO。

Here's the jQuery/JavaScript: 这是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
    });
}

The person pojo: 人物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;
    }
}

And here's the the controller: 这是控制器:

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);
    }
}

You can try a DIY approach with Spring MVC controllers and JSONObject from json.org. 您可以尝试使用Spring MVC控制器的DIY方法和来自json.org的JSONObject Just use it to json-serialize your returned objects and flush it down the response, with the appropiate headers. 只需使用它来json序列化您返回的对象并使用适当的标题将其清除响应。

It has its pitfalls (I would recommend you use a simple wrapper class with a getter when you try to send a collection), but I have been happy with it for some years. 它有它的缺陷(我建议你在尝试发送一个集合时使用一个带有getter的简单包装类),但是我已经很满意它了好几年了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM