JSCan not deserialize instance of java.lang.X out of START_OBJECT

342 阅读1分钟

一、 远程客户端返回的数据类型与接收类型不一致

二、 spring java 加了 produces = "application/json;charset=UTF-8" 返回 布尔值(Boolean)

Caused by: feign.codec.DecodeException: Error while extracting response for type [class java.lang.Boolean] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.lang.Boolean out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.Boolean out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1] at feign.AsyncResponseHandler.decode(AsyncRespons

原因:spring java 加了 produces = "application/json;charset=UTF-8" 返回 布尔值(Boolean)

这个问题是因为在Spring框架中,当使用@RequestMapping@GetMapping等注解时,如果返回值是java.lang.Boolean类型,Spring会默认将其转换为JSON格式。但是,当你设置了produces = "application/json;charset=UTF-8"后,Spring期望返回的是一个JSON对象,而不是一个简单的布尔值。

为了解决这个问题,你可以将返回值改为一个包含布尔值的JSON对象。例如:

 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping(value = "/myEndpoint", produces = "application/json;charset=UTF-8")
    public ResponseEntity<Boolean> myMethod() {
        // ...你的业务逻辑...
        boolean result = true; // 假设这是你的结果
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}


    

这样,你的接口将返回一个包含布尔值的JSON对象,而不是一个简单的布尔值。

其他错误:

JSON parse error: Can not deserialize instance of java.lang.Double out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException 这个错误信息是Java应用程序在使用Jackson库处理JSON数据时出现的一个典型异常。Jackson是Java中广泛使用的JSON序列化/反序列化库。