slartidan
slartidan

Reputation: 21576

JSON-Request to Jersey causes "400 Bad Request"

I have a jersey server example, that works fine with XML, but does not work with JSON.

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;

@Path("JsonExample")
public class JsonExample {

    @XmlRootElement
    public static class Input {
        public String text;
    }

    @POST
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public String test(Input i) {
        return i.text;
    }

}

If I send this xml request as application/xml to the server, everything works fine, the response is a (to http://localhost:8080/App/rest/JsonExample as POST)

<input><text>a</text></input>

I translated it to JSON at utilities-online.info and sent it as application/json to the same URL, with same parameters, but got an error "400 Bad Request"

{
  "input": { "text": "a" }
}

Environment:

What I've tried:

Upvotes: 0

Views: 3420

Answers (1)

slartidan
slartidan

Reputation: 21576

While writing my question I saw an exception, that "input" is not expected as a field. The correct JSON-request has to be:

{ "text": "a" }

Upvotes: 1

Related Questions