传递JSON数组进行查看

时间:2017-12-12 09:24:29

标签: spring

如何从请求API中获取的JSON数组中的值中列出View中的值?我正在使用spring mvc和thymeleaf

1 个答案:

答案 0 :(得分:1)

您可以使用注释@RequestBody来获取您的jsonArray并使用包含模型列表的包装器存储您的列表。 如下:

型号:

public class Tuple {
private String key;
private String value;
private String description;
public String getKey() {
    return key;
}
public void setKey(String key) {
    this.key = key;
}
public String getValue() {
    return value;
}
public void setValue(String value) {
    this.value = value;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

public Tuple(String key, String value, String description) {
    this.key = key;
    this.value = value;
    this.description = description;
}

}

打包机:

public class TupleWrapper{
    private List<Tuple> tupleList;
    public List<Tuple> getTuple() {
        return tupleList;
    }
    public void setTuple(List<Tuple> tuples){
        this.tupleList = tuples;
    }
}

控制器:

@RequestMapping(value="/setTuple", method = RequestMethod.POST)
public ResponseEntity<ResponseStatus> setTuple(@RequestBody TupleWrapper wrapper){
    //your logic.
    return new ResponseEntity<ResponseStatus>(new ResponseStatus<>(STATUS.SUCCESS,"OK",wrapper),HttpStatus.OK);

}