Spring Rest Template + MapStruct 400 BAD_REQUEST错误

时间:2019-03-14 10:35:08

标签: java spring rest mapstruct

具有以下实现:

    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("key", "72hdjas721398721");

    Application application = appRepository.findById(customerId)
            .get();
    BasicRequestMapper mapper = Mappers.getMapper(BasicRequestMapper.class);
    BasicRequest basicRequest = mapper.customerToBasicRequest(application);

    HttpEntity<BasicRequest> entity = new HttpEntity<>(basicRequest, headers);      
    try {
        ResponseEntity<Response> response = restTemplate.exchange(url, HttpMethod.POST, entity,
                Response.class);
        return response;
    } catch (HttpClientErrorException e) {
        System.out.println(e.getResponseBodyAsString());
    }

实现的简短摘要:我正在使用Springs RestTemplate来执行POST请求,以在其上发送POJO。

执行交换时,出现以下错误:

2019-03-14 11:19:51.300 DEBUG 7448 --- [           main] o.s.web.client.RestTemplate              : Response 400 BAD_REQUEST
{"key":"unknown.error.found","message":"Unexpected end-of-input in field name\n at [Source: java.io.PushbackInputStream@42450be5; line: 1, column: 1523]"}

此消息已由服务器端处理,因此我在错误消息中得到了““ key”:“ unknown.error.found”,“ message”。但是第二部分似乎是Jackson抛出的错误反序列化邮件时。

我要发送的对象是一个简单的POJO,几乎没有带有getter和setter的属性。我正在使用mapstruct,以便仅将我想要的属性从实体映射到此pojo。

示例:2019-03-14 11:19:50.763 DEBUG 7448 --- [ main] o.s.web.client.RestTemplate : Writing [BasicRequest [bankAccount=BankAccountDto [iban=DE12500105170648489890, holder=1]] as "application/json"

可以请教吗?

1 个答案:

答案 0 :(得分:0)

查看调试输出

2019-03-14 11:19:50.763 DEBUG 7448 --- [           main] o.s.web.client.RestTemplate              : Writing [BasicRequest [bankAccount=BankAccountDto [iban=DE12500105170648489890, holder=1]] as "application/json"

似乎RestTemplate没有发送我们的JSON内容。不确定您的依赖情况如何。但是,很可能您会缺少杰克逊作为依赖项。如果添加它,它将发送正确的数据。