简体   繁体   中英

webservice response “Unsupported Media Type” with POST method

I'm trying to understand Webservice in Java using Jersey. I follow the tutorial: http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

But I have some problem with my example.

Here is the code in webservice,

@POST
    @Path("/abc")
    @Consumes({MediaType.APPLICATION_JSON,
        MediaType.APPLICATION_FORM_URLENCODED
        })
    public Response createTrackInJSON(Track track) {
        String result = "Track saved : " + track.toString();
        System.out.println(result);
        return Response.status(201).entity(result).build();

    }

and here is from client:

var newTrack = {
                    title : "aaaa",
                    singer : "bbbb"
                };

             $.ajax({
                url : 'http://localhost:8080/Teamp3/rest/hello/abc/',
                type : 'POST',
                data :  { track : JSON.stringify(newTrack)},

                success : function(result) {
                    alert("success");
                    document.getElementById("lblRes_POST").innerHTML = result;
                },
                error : function(jqXHR, textStatus, errorThrown) {
                    alert("jqXHR - " + jqXHR.statusText + "\n" + "textStatus - "
                            + textStatus + "\n" + "errorThrown - " + errorThrown);

                }
            });

it will response: "UNSUPPORTED MEDIA TYPE"

if I put : contentType: 'application/json', dataType: 'json',

in the ajax code, it still notice that message. Could you please to give me some solutions for that. Thank you.

Try to change client side as below:

  1. Change data : { track : JSON.stringify(newTrack)} to data : JSON.stringify(newTrack)

For better suggestion, please sent us the network capture on network tab.

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM