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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘