0

When I try to send data to server by POST method, I got this error on the server side:

java.lang.IllegalArgumentException: "json" does not contain '/'.

First I thought that date format from jQuery UI DatePicker is wrong and I changed it to "yy-mm-dd", but unfortunately is still does not work. I am really confused, because I don't know where is '/' which makes me trouble. I would be very happy if anybody decides to help me - Thank you

This is error, $.ajax and mapping are also below:

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

That is how I send data:

$.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)
      }
  });

And here is how I try to map it in 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{
(....)

UPDATE: now this is the code:

$.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()
  }
});

And mapping:

@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{

And now I got: POST http://localhost:8084/pracainz/event/addEvent 400 (Bad Request)

1 Answer 1

1

The error message is pretty clear: MediaType.parseMediaType tries to parse the "json" String literal into a MediaType but throws an exception because it's not a valid media type name. The media type of JSON is application/json. Since all valid internet media types contain two parts separated by '/' all input without this character is guaranteed to be invalid.

This:

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

should be:

$.ajax({
       url: _getContextPath() + "/event/addEvent",
       method: "POST",
       contentType: "application/json",
       // ...
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for help, I updated my post with your changes, but now I got: POST localhost:8084/pracainz/event/addEvent 400 (Bad Request), still something is wrong.
That's another question, isn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.