Spring Rest Template + MapStruct 400 BAD_REQUEST错误

问题描述 投票:0回答:1

具有以下实施:

    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]"}

此消息已由服务器端处理,因此我在错误消息中得到“”密钥“:”unknown.error.found“,”message“。但第二部分似乎是杰克逊错误抛出的时候序列化消息。

我发送的对象是一个简单的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"

你能给些建议么?

java spring rest mapstruct
1个回答
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内容。不确定你的附属品是怎样的。但是,很可能你错过了杰克逊作为依赖。如果你添加它,它应该发送适当的数据。

© www.soinside.com 2019 - 2024. All rights reserved.