dojo 将对象列表从客户端传递到Spring控制器

x33g5p2x  于 2022-12-08  发布在  Dojo
关注(0)|答案(2)|浏览(160)

我在从客户端检索数据到控制器时遇到问题。
我收到400错误请求错误,但找不到原因。

我在这里使用dojo gridx,这是js文件

function goToReviewTargetsPage(){
    var gridSelectedData = JSON.stringify( dijit.byId("geneGridNodeforSelectTargets").store.data);

    dojo.xhrPost({
        url : contextPath + '/updateGeneData.htm',
        handleAs: 'json',
        headers: { 
                'Content-Type': 'application/json',
            }, 
        applyAroundAdviceOnLoad : true,
        preventCache : true,
        content : 
        {
            "selectedTargetsData" : gridSelectedData
        },
        load : function(data) 
        {
          \\do something
        },
        error : function(error) 
        {
            hideLoadingDialog();
        }
    });
}

上述js中提到的gridSelectedData中的json数据为:

[{"id":1,"name":"ABL1","diseases":[{"id":1,"diseaseName":"CANCER"}],"SV":true,"CNV":true,"SNP":true},{"id":2,"name":"ALK","diseases":[{"id":1,"diseaseName":"CANCER"}],"SV":false,"CNV":true,"SNP":true},{"id":3,"name":"BLA","diseases":[{"id":1,"diseaseName":"CANCER"}],"SV":false,"CNV":false,"SNP":true},{"id":4,"name":"ALS","diseases":[{"id":2,"diseaseName":"LUNGS"}],"SV":false,"CNV":true,"SNP":true}]

这是来自控制器的代码

@RequestMapping(value = "/updateGeneData", produces = MediaType.APPLICATION_JSON_VALUE,
    consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
@ResponseBody
public List<Gene> getSelectedGenes(@RequestBody Gene[] genes,
    HttpServletResponse response, HttpServletRequest request)
{
    List<Gene> geneList = new ArrayList<>();
    // do something
    return geneList;
}

基因.java(POJO)

public class Gene {

private Integer id;
private String name;
private boolean SNP = false;
private boolean SV = false;
private boolean CNV = false;
Set<Disease> diseases = new HashSet<Disease>();
 //getters and setters
}

这是请求标题

Accept:*/* Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.9 ajaxRequest:true Connection:keep-alive Content-Encoding:UTF-8 Content-Length:795 Content-Type:application/json X-Requested-With:XMLHttpRequest

有效负载为:

[{id: 1, name: "ABL1", diseases: [{id: 1, diseaseName: "CANCER"}], SV: true, CNV: true, SNP: true},…]
avwztpqn

avwztpqn1#

在/updateGeneData上更改来自/updateGeneData. htm的发布请求并删除属性产生=媒体类型.APPLICATION_JSON_VALUE,使用=媒体类型.APPLICATION_JSON_VALUE,

vjrehmav

vjrehmav2#

你的控制器需要一个array [],而你传递给它一个list。你的控制器应该是

@RequestMapping(value = "/updateGeneData", 
                produces MediaType.APPLICATION_JSON_VALUE, 
                consumes = MediaType.APPLICATION_JSON_VALUE, 
                method = RequestMethod.POST)
@ResponseBody 
public List<Gene> getSelectedGenes(@RequestBody List<Gene> genes) {
    List<Gene> geneList = new ArrayList<>();
    // do something
    return geneList;
}

我发现有时我需要 Package 我的DTO才能让Jackson工作。这种方法在过去对我很有效。

class Genes extends ArrayList<Gene> { }

@ResponseBody 
public List<Gene> getSelectedGenes(@RequestBody Genes genes) {

相关问题