- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我尝试使用以下输入在 java Spring boot 中使用 boolean 字段的无效值发布消息时,出现 HttpMessageNotReadableException:
{
"User":"xyz",
"email":"xyz@email.com",
"isAdmin":"No" ( This is Boolean in my API Bean expecting true or false)
}
我分别在控制台和响应正文中收到以下错误消息:
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'yes': was expecting ('true', 'false' or 'null')at [Source: (PushbackInputStream); line: 29, column: 29]
以下响应正文:
{
"Code": "JSON_REQUEST_INVALID",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Unrecognized token 'yes': was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token 'yes': was expecting ('true', 'false' or 'null')\n at [Source: (PushbackInputStream); line: 29, column: 7] (through reference chain: com.sample.model.apibeans.userDetails[\"user\"])",
"path": "/userapi/v1"
}
但是我希望错误消息更具体,例如字段“isAdmin”中不允许"is"!因为上面的消息没有提供有关哪个字段导致错误的太多信息。
我在下面使用了 @ControllerAdvice 来捕获并覆盖该消息,但是我无法获取发生此错误的字段。下面是异常处理方法的实现。
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
@ResponseBody
protected Errors handleHttpMessageNotReadableException(InputMismatchException ex, WebRequest request) {
Errors errors = new Errors();
Error error;
error = new Error();
error.setParams(null);
error.setMessage("Json Contains "+ex.getCause());
error.setTraceId(request.getHeader(TRACE_ID_HEADER_NAME));
error.setStatus(Integer.toString(HttpStatus.BAD_REQUEST.value()));
error.setCode("JSON_REQUEST_INVALID");
errors.addErrorsItem(error);
return errors;
}
Ex 对象不提供任何方法来获取发生此情况的路径或字段。我如何获取该信息以便向消费者提供更具体的错误消息。
注意:我使用的是 Spring Boot 2.2.1
我已经在 Stack-overflow 上看到了其他类似的问题,但没有一个能够解决这个问题。
最佳答案
好吧,我还没有获得 java 方法来获取确切的字段,但是由于错误的模式,我得出了以下可能有用的字段提取的结论。
String param= (String) ex.getMessage().subSequence(ex.getMessage().lastIndexOf("[")+1,ex.getMessage().lastIndexOf("]"));
这会获取发生错误的确切字段,并向客户端提供动态消息,如下所示:
{
"Code": "JSON_REQUEST_INVALID",
"status": 400,
"error": "Bad Request",
"message": "unexpected value at isAdmin",
"path": "/userapi/v1"
}
关于java - 异常:com. fastxml.jackson.core.JsonParseException:HttpMessageNotReadableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751982/
我有一个这样的 DTO 对象: data class ClientDto( var uuid: String, var ip: String, var
当我尝试在 java 中使用 spring 执行 get 时,出现 HttpMessageNotReadableException。打印的 Stacktrace 说: 嵌套异常是 org.spring
我仍在学习测试方法,我正在尝试让 MockMvc 测试为我工作。这是一个简单的 REST Controller ,此时仅使用帖子中 json 中的信息进行一些身份验证。我实际上已经实现了代码,所以我知
我正在用 postman 尝试这段代码,但没有任何效果,为什么? 我寄什么: { "name":"front_msel", "gitlabId": "83", "fichier
我有一个带有双字段的表单。当我添加一个不是 Double ('foo') 的值时,它应该给我一个 BeanPropertyBindingResult ,其中包含该字段的错误。但我得到了一个HttpMe
我有以下异常处理程序: @ControllerAdvice public class MyExceptionHandler { private static final Logger logger
我正在尝试使用 postman 并将这些值放入数据库中,但我不断收到异常。 我试图从 postman 反序列化什么: { "end_date": "2443-11-34 12:43:23",
我在处理 json 格式的 Rest API 请求时遇到问题,该请求无法转换。请求代码如下所示: headers = {"content-type": "application/json","user
我有这个类来代表用户: @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) @
我有一个 API,需要以下 dto 结构。 data class SignupRequest( @NotEmpty(message ="Username must not be empty")
我有以下 mock mockMvc.perform(get("/bk/getBks?sd="+1262296800000L+"&nod=3")) .andDo(print()); 这是我的 C
我正在使用 Spring boot 并使用过滤器过滤请求。该过滤器用于检查用户验证,该验证正在从请求正文中读取一些数据(为此我使用了 HttpServletRequestWrapper 实现)。从 r
我想访问 HttpMessageNotReadableException 中的 _path 属性,但我不知道通过哪个或哪些方法来执行此操作。我怎样才能访问它? 我的JDK版本是JDK8,我的Sprin
我操作API服务器。 手机APP访问API服务器时,有时会出现该异常。 我尝试在测试服务器上进行测试,但无法重现。(我改变了apache和tomcat的连接时间。) 有什么问题?? 我该如何解决这个问
我正在尝试对我在 Controller 中创建的发布请求进行 JUNIT 测试。虽然 GET 成功,但 POST 不成功。 public class SampleObject implements S
我向我的网络服务添加了以下依赖项 org.springframework.cloud spring-cloud-starter-net
我正在尝试使用@RequestBody @Valid 注释在 Spring 中进行 bean 验证和数据映射。 正如您在实体 bean 中看到的那样,我使用了 @NotNull 和 @Min 注释,它
我正在使用 springframework -RestTemplate,为了发出 get 请求,并将 xml 响应转换为 java 对象。运行后:RestTemplate.exchange,我遇到以下
我有一个包含几个方法的 Controller 类,其中一个方法应该接受 POST 请求并使用来自该 POST 请求正文的 JSON 创建一个新帐户。 当我尝试使用 curl 发出 POST 请求时,我
这是我的客户端代码 RestTemplate restTemplate = new RestTemplate(); ResponseEntity responseEntity = re
我是一名优秀的程序员,十分优秀!