当我尝试通过POST方法将数据发送到服务器时,我在服务器端遇到了这个错误:
java.lang.IllegalArgumentException: "json" does not contain '/'.
首先我认为来自jQuery UI DatePicker的日期格式是错误的,我将其更改为" yy-mm-dd
",但不幸的是仍然无效。我真的很困惑,因为我不知道'/'
在哪里让我遇到麻烦。如果有人决定帮助我,我会很高兴 - 谢谢你
这是错误,$ .ajax和映射也在下面:
java.lang.IllegalArgumentException: "json" does not contain '/'
at org.springframework.http.MediaType.parseMediaType(MediaType.java:648)
at org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition$ConsumeMediaTypeExpression.matchMediaType(ConsumesRequestCondition.java:215)
at org.springframework.web.servlet.mvc.condition.AbstractMediaTypeExpression.match(AbstractMediaTypeExpression.java:63)
at org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition.getMatchingCondition(ConsumesRequestCondition.java:165)
at org.springframework.web.servlet.mvc.method.RequestMappingInfo.getMatchingCondition(RequestMappingInfo.java:174)
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getMatchingMapping(RequestMappingInfoHandlerMapping.java:64)
// snip
这就是我发送数据的方式:
$.ajax({
url: _getContextPath() + "/event/addEvent",
method: "POST",
contentType: "json",
data: {
"ownerId": $("#ownerIdHidden").val(),
"title": $("#title").val(),
"place": $("#place").val(),
"startdate": $("#startDate").val(),
"starthour": $("#startHour").val(),
"duration": $("#duration").val(),
"description": $("#eventDescription").val(),
"invited": _getLoginsFromTable(invitedTable)
}
});
以下是我尝试在Spring MVC Cotroller中映射它的方法:
@ResponseBody
@RequestMapping(value="event/addEvent", method=RequestMethod.POST,
consumes="application/json")
public String addEvent(@RequestParam("ownerid") Long ownerid, @RequestParam("title") String title,
@RequestParam("place") String place, @RequestParam("startdate") Date startDate,
@RequestParam("starthour") Integer startHour, @RequestParam("duration") Integer duration,
@RequestParam("description") String description, @RequestParam("invited") String[] invitedLogins){
Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
try{
(....)
更新:现在这是代码:
$.ajax({
url: _getContextPath() + "/event/addEvent",
method: "POST",
contentType: "application/json",
data: {
"ownerId": 3,
"title": 3,
"place": $("#place").val(),
"startdate": (new Date($("#startDate").val())).getTime(),
"starthour": $("#startHour").val(),
"duration": $("#duration").val(),
"description": $("#eventDescription").val()
}
});
并映射:
@ResponseBody
@RequestMapping(value="event/addEvent", method=RequestMethod.POST,
consumes="application/json")
public String addEvent(@RequestParam("ownerid") Long ownerid, @RequestParam("title") String title,
@RequestParam("place") String place, @RequestParam("startdate") Long startDateMilis,
@RequestParam("starthour") Integer startHour, @RequestParam("duration") Integer duration,
@RequestParam("description") String description){
Session session = NewHibernateUtil.getSessionFactory().getCurrentSession();
try{
现在我得到了:POST http://localhost:8084/pracainz/event/addEvent 400 (Bad Request)
答案 0 :(得分:1)
错误消息非常清楚:MediaType.parseMediaType
尝试将" json" 字符串文字解析为MediaType
,但会抛出异常,因为它&# 39; s不是有效的媒体类型名称。 The media type of JSON是application/json
。由于所有有效的互联网媒体类型都包含由' /
'分隔的两个部分。所有没有此字符的输入都将被保证无效。
此:
$.ajax({
url: _getContextPath() + "/event/addEvent",
method: "POST",
contentType: "json",
// ...
应该是:
$.ajax({
url: _getContextPath() + "/event/addEvent",
method: "POST",
contentType: "application/json",
// ...