简体   繁体   English

带有其他对象列表的Spring Web服务请求和响应映射

[英]Spring web service request and response for mapping having list of other objects

I am using Spring web service and in my controller I am using @RequestBody and @ResponseBody. 我正在使用Spring Web服务,在我的控制器中,我正在使用@RequestBody和@ResponseBody。 Now from what I understand these annotations do the magic of converting the incoming request to the class object that you specify. 现在,根据我的理解,这些注释可以将输入的请求转换为您指定的类对象。 However, what if my class object had references to other class objects. 但是,如果我的类对象引用了其他类对象该怎么办。 Something like: 就像是:

public class Question {

    private String questionText;

    List<Options> options;

    public String getQuestionText() {
        return questionText;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }

    //getters and setters for options


}

The incoming request can look something like this: 传入的请求可能如下所示:

{"questionText":"sample question","options":{"option-0":"option0","option-1":"option1","option-2":"option2","option-3":"option3"}}

Option looks something like this: 选项看起来像这样:

public class Option {

    private String option;

    public String getOption() {
        return option;
    }

    public void setOption(String option) {
        this.option = option;
    }



}

How can will this be mapped? 如何将其映射?

However, what if my class object had references to other class objects. 但是,如果我的类对象引用了其他类对象该怎么办。

This is absolutely not an issue. 这绝对不是问题。 Jackson, which Spring uses, can extract that information to produce the appropriate JSON. Spring使用的Jackson可以提取该信息以生成适当的JSON。

Your Question class acts as the template for the root JSON. 您的Question类充当根JSON的模板。 So the JSON object will have a field called questionText which will be a JSON String and a field called options which will be a JSON array with JSON objects in it that follow the Options template. 因此,JSON对象将具有一个名为questionText的字段(将是一个JSON字符串)和一个名为options的字段,这将是一个带有遵循Options模板的JSON对象的JSON数组。

Consequently, this 因此,这

"options":{"option-0":"option0","option-1":"option1","option-2":"option2","option-3":"option3"}

is invalid. 是无效的。 options must be a JSON array and the elements must be JSON objects, not JSON strings. options必须是JSON数组,并且元素必须是JSON对象,而不是JSON字符串。

It would have to look like 它必须看起来像

"options":[{"option":"option1"}, {"option":"option2"}]

to match your Options class. 匹配您的Options类。


Knowing that Spring uses Jackson, you can test this relatively easily 知道Spring使用Jackson,您可以相对轻松地进行测试

ObjectMapper mapper = new ObjectMapper();
Options o1 = new Options();
o1.setOption("option1");
Options o2 = new Options();
o2.setOption("option2");
Question question = new Question();
question.setOptions(Arrays.asList(o1, o2));
question.setQuestionText("sample question");
System.out.println(mapper.writeValueAsString(question));

produces 产生

{"questionText":"sample question","options":[{"option":"option1"},{"option":"option2"}]}

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

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