JeffYL 2015-08-05 06:01 采纳率: 0%
浏览 6469
已结题

紧急!!求大神帮忙!Spring MVC使用@RequestBody 接收带空格的字符串,报错。

实体类:
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条回答 默认 最新

  • xiaobai111 2015-08-05 07:51
    关注

    把你返回的字符串转为json字符串输出

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3