解决springMVC接收json数据报错:Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

springMVC中的controller方法:

@ResponseBody
@PostMapping("/proxyInterfaceInfo")
public RpcResponse<ProxyInterfaceInfoRpcResponse> proxyInterfaceInfo(@RequestBody ProxyInterfaceInfoRpcRequest gwRequest) {

return null;
}

controller参数类:

@Setter
@Getter
public class ProxyInterfaceInfoRpcRequest extends BaseRequest{
	private static final long serialVersionUID = -5187407229562570847L;
	private Long subsidyId;
	private String interfaceCode;
	private String jsonData;
}

客户端请求:
解决springMVC接收json数据报错:Cannot deserialize instance of `java.lang.String` out of START_OBJECT token_第1张图片
客户端发送请求后,controller报异常:

2019-12-29 17:42:20.110 [http-nio-9986-exec-2] WARN  o.s.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver 140 - Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 54] (through reference chain: com.poly.pc.gateway.v1_0.struct.subsidy.request.ProxyInterfaceInfoRpcRequest["jsonData"])]

解决办法:
controller参数类ProxyInterfaceInfoRpcRequest的属性jsonData的类型改为Object即可:

@Setter
@Getter
public class ProxyInterfaceInfoRpcRequest extends BaseRequest{
	private static final long serialVersionUID = -5187407229562570847L;
	private Long subsidyId;
	private String interfaceCode;
	private Object jsonData;
}

可以这么理解,客户端提交的参数:

{"subsidyId":14,"interfaceCode":"TDJK001","jsonData":{"code":"Test001","muser":"00000000001"}}

这里的jsonData也被认为是一个复杂对象,而不是简单的String字符串。

你可能感兴趣的:(springMVC)