Controller in Springboot project accepts List<Object> parameters

Methods for controllers to accept complex parameters

Recently, when I was writing a request request for a WeChat applet, I found that the backend kept reporting errors. It seemed that the array parameter sent by the frontend and the parameter List<Integer> accepted by the backend could not correspond. I searched many methods on the Internet but could not solve the problem. If you change the solution, change the error report, including but not limited to

No primary or single unique constructor found for interface java.util.List

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.util.ArrayList<java.lang.Integer> from Object value (token JsonToken.START_OBJECT); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.ArrayList<java.lang.Integer> from Object value (token JsonToken.START_OBJECT) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]]

While waiting for various error reports, I kept switching to content-typeand from various JSON.stringifyback-end Alibaba Fastjsonsolutions, but none of them solved the problem. But in the end, I still referred to a blog post and solved the problem by creating a new data object. I still have too little experience. I still need to learn other solutions if I have the opportunity in the future.

Pass complex parameters using data class methods

Create a new data class for passing parameters

public class TheIDList implements Serializable {
    
    
	//这里我只用到一个List<Integer>参数,故这个数据类只写了一个属性
	//如果有需要的话可以自定义其他数据类型如Map等
    private List<Integer> idList;

    public List<Integer> getIdList() {
    
    
        return idList;
    }
    public void setIdList(List<Integer> idList) {
    
    
        this.idList = idList;
    }
}

Controller

    @PostMapping("/test")
    @ResponseBody
    public Returntype methodName(@RequestBody TheIDList theIDList ){
    
    
        List<Integer> intList = theIDList.getIDList();
        ***
        methodbody
        ***
        return returntype 
    }

WeChat applet front-end code

    wx.request({
    
    
      url: 'http://localhost:8080/test',
      method: 'POST',
      header: {
    
    
        'content-type': 'application/json'
        },
      data:{
    
    
      	//这里idList与数据类中的属性对应
        idList:IDList
      },
      success : function(res){
    
    
      	console.log("success")
        })
      }
    })

The method used refers to the following article
https://blog.csdn.net/Tach1banA/article/details/118581214

Guess you like

Origin blog.csdn.net/qq_51330798/article/details/130312999