【问题标题】:java.lang.IllegalArgumentException: "json" does not contain '/'java.lang.IllegalArgumentException:“json”不包含“/”
【发布时间】:2014-05-11 12:55:10
【问题描述】:

当我尝试通过 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)

【问题讨论】:

    标签: java jquery ajax json spring


    【解决方案1】:

    错误消息非常清楚:MediaType.parseMediaType 尝试将 "json" 字符串文字解析为 MediaType,但由于它不是有效的媒体类型名称而引发异常。 The media type of JSONapplication/json。由于所有有效的互联网媒体类型都包含由“/”分隔的两部分,所有没有此字符的输入都保证无效。

    这个:

    $.ajax({
           url: _getContextPath() + "/event/addEvent",
           method: "POST",
           contentType: "json",
           // ...
    

    应该是:

    $.ajax({
           url: _getContextPath() + "/event/addEvent",
           method: "POST",
           contentType: "application/json",
           // ...
    

    【讨论】:

    • 感谢您的帮助,我用您的更改更新了我的帖子,但现在我得到了:POST localhost:8084/pracainz/event/addEvent 400(错误请求),仍然有问题。
    • 那是另一个问题,不是吗?
    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2017-01-06
    • 2018-03-01
    • 2022-08-24
    相关资源
    最近更新 更多