An HttpMessageNotReadable exception is HttpMessageNotReadable , and I just couldn't see it, because I didn't write or print it anywhere. I found it by building an HTTP request in my test class using the DefaultRequestBuilder class and adding andDo(print()) :
DefaultRequestBuilder requestBuilder = MockMvcRequestBuilders.post("/debug").contentType(MediaType.APPLICATION_JSON).body(new String("{\"T1\":109.1, \"T2\":99.3}").getBytes()); this.mockMvc.perform(requestBuilder).andDo(print());
So after that, using the output of andDo(print()) , I could see that the HttpMessageNotReadable exception was thrown, but did not know the details of the exception or what caused it. To see the details, I had to add this to the controller class in order to write the exception details in the response body:
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseBody public String handleException1(HttpMessageNotReadableException ex) { return ex.getMessage(); }
This revealed the following exception:
Could not read JSON: Unrecognized field "T1" (Class com.company.project.model.device.DebugOutput), not marked as ignorable
which I fixed by adding @JsonProperty annotation to the setters in my model class:
@JsonProperty("T1") public void setT1(Float t1) { T1 = t1; }
CFL_Jeff
source share