java 无法读取 JSON:字段名称中出现意外的输入结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36720168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not read JSON: Unexpected end-of-input in field name
提问by Kasun Kariyawasam
I am developing a Spring MVC web application. I am not still develop the UI. So I am testing my services using Advance Rest Client tool.
我正在开发一个 Spring MVC Web 应用程序。我还没有开发 UI。所以我正在使用 Advance Rest Client 工具测试我的服务。
My Controller
我的控制器
@Controller
@RequestMapping("/testController")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public
@ResponseBody void testMethod(@RequestBody TestParam testParam) {
String tenant = testParam.getTenantCode();
String testString = tenant + " is the tenant";
}
}
TestParam.java class
TestParam.java 类
public class TestParam {
private String testVar;
private String tenantCode;
public String getTenantCode() {
return tenantCode;
}
public void setTenantCode(String tenantCode) {
this.tenantCode = tenantCode;
}
public String getTestVar() {
return testVar;
}
public void setTestVar(String testVar) {
this.testVar = testVar;
}
}
I send the request using Advance Rest Client and headers and request link has set correctly.
我使用 Advance Rest Client 发送请求,标题和请求链接已正确设置。
{"testVar":"Test","tenantCode":"DEMO"}
Request link
请求链接
http://localhost:8080/myApp/controller/testController/test
It works correctly when TestParam has one veriable. When it becomes two or more it gives an Error and it not hit the testMethod.
当 TestParam 有一个可验证的时,它可以正常工作。当它变成两个或更多时,它会给出错误并且不会命中 testMethod。
exception is com.fasterxml.Hymanson.core.JsonParseException: Unexpected end-of-input in field name at [Source:org.apache.catalina.connector.CoyoteInputStream@7b24d498; line: 1, column: 43]
at org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter.readJavaType(MappingHymanson2HttpMessageConverter.java:181)
at org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter.read(MappingHymanson2HttpMessageConverter.java:173)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:135)
I went throw more articles and I still couldn't find the answer.
我去扔了更多的文章,但我仍然找不到答案。
回答by Kasun Kariyawasam
Increasing Content-Length:
in header
works
增加Content-Length:
的header
工作
回答by EL missaoui habib
Whats your json format ? I think json format uses literal \n's as delimiters, please be sure that the JSON actions and sources are not pretty printed.
你的json格式是什么?我认为 json 格式使用文字 \n 作为分隔符,请确保 JSON 操作和源没有被很好地打印出来。
回答by Pawe? Pszty?
There is an issue in ARC where there is no payload and the response is of a type of json. Parser is throwing error because the string is empty and the response report is crashing.
ARC 中存在一个问题,即没有有效负载并且响应属于 json 类型。解析器抛出错误,因为字符串为空并且响应报告崩溃。