错误:422无法处理的实体(Postentity RestTemplate)

时间:2019-11-13 04:03:54

标签: java spring-boot

我正在尝试调用post API方法并由邮递员对其进行测试,但是API无法正常工作。调试时,出现错误422无法处理的实体,我不知道API中的问题是什么,请帮助我:

我的json文件:

{
   "issue": {
      "project_id": 146,
      "tracker_id":5,
      "status_id": 1,
      "priority_id":2
     }
}

模型

@JsonRootName(value = "issue")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class IssueModel {

    @JsonProperty(value = "project_id")
    private int project;
    @JsonProperty(value = "tracker_id")
    private int tracker;
    @JsonProperty(value = "status_id")
    private int status;
    @JsonProperty(value = "priority_id")
    private int priority;

控制器

@RequestMapping(value = "/issue", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
@ResponseBody
public String callPostRequest(@RequestBody MapModel requestmodel) {
    RestTemplate restemplate = new RestTemplate();
    ObjectMapper mapper = new ObjectMapper();
    String mapentity = null;
    try {
        mapentity = mapper.writeValueAsString(requestmodel);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    restemplate.getInterceptors().add(new BasicAuthorizationInterceptor("user", "passs"));
    HttpHeaders header = new HttpHeaders();
    header.set("content-type", MediaType.APPLICATION_JSON_UTF8_VALUE);

    HttpEntity<String> requestbody = new HttpEntity<>(mapentity, header);

    ResponseEntity<MapModel> result = restemplate.postForEntity(URl, requestbody, MapModel.class);
    if (result.getStatusCode() == HttpStatus.OK) {
        return result.getBody().toString();
    }
    return null;

0 个答案:

没有答案