JeffYL
2015-08-05 06:01紧急!!求大神帮忙!Spring MVC使用@RequestBody 接收带空格的字符串,报错。
5实体类:
public class TestVO {
private String CreateDate;
public String getCreateDate() {
return CreateDate;
}
public void setCreateDate(String createDate) {
CreateDate = createDate;
}
}
Controller:
@RequestMapping(value="/test2/", headers="Accept=application/json")
@ResponseBody
public TestVO test2(@RequestBody TestVO user) {
return user;
}
使用CURL发送请求:
curl -H "Content-Type: application/json" -d {\"createDate\":\"2015-07-31 00:00:00\"} --request POST http://localhost:8081/HQ520/test2/
报的错误:
八月 05, 2015 1:46:35 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [rest] in context with path [/HQ520] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected end-of-input in VALUE_STRING
at [Source: org.apache.catalina.connector.CoyoteInputStream@2e330425; line: 1, column: 91]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected end-of-input in VALUE_STRING
at [Source: org.apache.catalina.connector.CoyoteInputStream@2e330425; line: 1, column: 91]] with root cause
com.fasterxml.jackson.core.JsonParseException: Unexpected end-of-input in VALUE_STRING
我测试过了,应该是空格的问题,以下是我的单元测试
@Test
public void testInsertObject() throws Exception {
String url = URI + "/object";
TestVO anObject = new TestVO();
anObject.setCreateDate("2015-07-31 00:00:00");
Gson gson = new Gson();
String json = gson.toJson(anObject);
MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.post(url).contentType(MediaType.APPLICATION_JSON)
.content(json))
.andDo(print())
.andReturn();
}
这样会报错,如果改成nObject.setCreateDate("2015-07-31-00:00:00"); 就不会报错了
我认为应该是MappingJacksonHttpMessageConverter解析JSON字符串的时候,如果遇到空格就认为这个字符串结束,所以出现这个问题。
有哪位大神遇到过类似的问题?怎么解决?请指教。谢谢!
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- 淘淘商城Spring注入报错求大神帮看看啊!!!跪求,看了好久了没解决
- spring
- java
- bean
- 注解
- 10个回答
- 求大神帮帮忙,项目启动报错如下:
- spring
- java
- 3个回答
- 关于字符串截取的问题,关键要快,简洁,急!!!
- java
- 字符串截取
- 2个回答
- 这个报错一直解决不了,求大神解决!!!!!
- 指针
- 1个回答
- 求大神帮忙,如何实现进销存系统权限控制!
- spring
- mvc
- 2个回答