没有字符串参数构造函数/工厂方法可从字符串值('22tp2tt490s')反序列化

3zwjbxry  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(315)

下面是rest控制器类,它是post请求

@PostMapping(value = "/details")
public SubSeriesDetailsRequest getGlobalSearchProductDetails(@RequestBody List<SubSeriesCodesRequest> codes,
        @QueryParam("contextString") String contextString) {

    LOGGER.info("Inside post request " + codes);

    String url = "http://www.example.com/details";
    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<Object> entity = new HttpEntity<>(headers);
    // SubSeriesDetailsRequest detailsRequest = new SubSeriesDetailsRequest();

    SubSeriesDetailsRequest response = restTemplate.postForObject(url, entity, SubSeriesDetailsRequest.class);
    return response;

}

我的dto类
@jsoninclude(jsoninclude.include.non\u null)@jsonignoreproperties(ignoreunknown=true)
公共类subscriescodesrequest实现可序列化{

private static final long serialVersionUID = 6880035785505252493L;
private List<String> codes;

public SubSeriesCodesRequest(List<String> codes) {
    super();
    this.codes = codes;
}

@JsonCreator
public SubSeriesCodesRequest() {

}

public List<String> getCodes() {
    return codes;
}

public void setCodes(List<String> codes) {
    this.codes = codes;
}

需要实现如下post请求

qqrboqgw

qqrboqgw1#

您的请求与预期模型不匹配。你是说你希望 ListSubSeriesCodesRequest 你正在发送一个字符串数组。
您当前的模型如下所示:

[
    {
        "codes": [
            "Code 1",
            "Code 2"
        ]
    }
]

试着把你的方法改成这样 public SubSeriesDetailsRequest getGlobalSearchProductDetails(@RequestBody SubSeriesCodesRequest request, 它应该能正常工作。

相关问题