Sending JSON to server is giving 415 Unsupported Media Type

。_饼干妹妹 提交于 2019-12-13 00:41:31

问题


I have the following angular code:

<button ng-click="ctrl.submit(light)">Switch</button>

and the button click is handled by:

   self.submit = function(light) {
        console.log("==============================");
        console.log("clicked button: ", light);
        console.log("==============================");

        $http.post('/ARHomeAutomation/rest/light/state/', light).then(function(response) {
            console.log('headers: ' , response.headers);
            console.log('status: ', response.status);
            console.log('config: ', response.config);
            console.log('data: ', response.data);
            self.state = response.data;
        }, function(errResponse) {
          console.error('Error while updating light state');
        })
        console.log('User clicked submit with: ', light.id );
        console.log('response: ', self.light);
    }

On the server side I have the following method that should respond to the request:

 @POST
 @Path("/state/")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.TEXT_HTML)
 public String setStateOfLight(JSONObject aLight) {
    if(aLight == null) {
      return "It's a null";
    } else {
      System.out.println("content: " + aLight);
    }
    JSONObject jsonObject = new JSONObject();
    //Update the state of the light with the given id
    for(Light light: lightCollection) {
        // .....
    }
    return getLightsStateAsJSON();
 }

When I click my button I get the following message:

According to firefox I do send JSON in my request. At least it says so when I examine the send data.

This is my header request data:

What am I missing here?


回答1:


"I downloaded Jersey 2.17 with all dependencies. AT least that what it says on their download site"

Yeah so the Jersey distribution (Jersey JAX-RS 2.0 RI bundle ) doesn't come bundled with an JSON conversion support, besides basic low level types, that can be converted from an InputStream. So without anything else beside the jars that come in that Jersey Bundle, the only type you have use are String, InputStream and byte[]. Doesn't really help much if you are trying to manipulate the JSON

How conversion works is through the use of MessageBodyReaders and MessageBodyWriters. I don't know what JSONObject is, but I'm guessing it's from here. In any case, you will need a MessageBodyReader for it to handle the incoming conversion. My guess is you don't have one. I personally don't know where to get one for that specific API.

I would instead make use of a library that can handle JSON to POJO mapping. Jackson is the one I would recommend. The basic are pretty easy to understand. Say this is your JSON

{"name":"your name", "id": "2"}

All you need to do is create a class with fields an properties, using JavaBean naming convention. So for the above JSON, we could use this

public class User {
    private String name;
    private String id;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
}

Now your method parameter can accept a User type.

To get this support, you need to add the Jackson provider. Download the below

  • jersey-media-json-jackson

  • Get all the one from the below image

    I had an image from another post with v2.2.3- disregard the version. The version you want to get 2.3.2 for all of them. They can all be found at the above link, for the first dependency. Just search for them in the search bar. When you find it, select the version and download it.

After adding these jars. you should have JSON to POJO support.




回答2:


It appears that the header content-type is set to text/html, even though the body is JSON. I imagine you will need to set the content-type to application/json somewhere in the post in the Angular code.



来源:https://stackoverflow.com/questions/29788461/sending-json-to-server-is-giving-415-unsupported-media-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!