restemplate post返回[400]no body

djmepvbi  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(1135)

以下方法在调用postforentity的行上对postman抛出500个错误(并在控制台上显示“[400]无正文”:

public URI save(ProjectDTO project, HttpServletRequest request) throws IntegrationException {
    URI uri = null;
    String url = String.format("%s/manager/v1/projects", hostUrl);
    ResponseEntity<ProjectDTO> responseEntity = null;

    try {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());

        String data = Converter.projectToJSONObject(project);
        HttpEntity<String> projectEntity = new HttpEntity<>(data, headers);

        ResponseEntity<ProjectDTO> projectResponse = restTemplate.postForEntity(url, projectEntity, ProjectDTO.class);
        ProjectDTO projectSave = projectResponse.getBody();

        return new URI(String.format("%s/%s", request.getRequestURL(), projectSave.getId()));
    } catch (Exception e ) {
        throw new IntegrationManagerException(e);
    }
}

dto使用 @JsonProperty :

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ProjetoDTO {

    @JsonProperty(value = "id")
    private String id;

    @JsonProperty(value = "projectId")
    private String projectId;

    // etc

}

引发的异常很简单:

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [no body]
 at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:184) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:449) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
 at br.com.projectapi.service.ProjectService.save(ProjectService.java:120) ~[classes/:na]
 at br.com.projectapi.controller.ProjectController.save(ProjectController.java:116) ~[classes/:na]

怎么了?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题