JSON发送到服务器是给415不支持的媒体类型 [英] Sending JSON to server is giving 415 Unsupported Media Type

查看:173
本文介绍了JSON发送到服务器是给415不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的角code:

 <按钮NG点击=ctrl.submit(光)>开关和LT; /按钮>

和按一下按钮被处理的:

  self.submit =功能(光){
        的console.log(==============================);
        的console.log(点击按钮:光);
        的console.log(==============================);        $ http.post('/ ARHomeAutomation / REST /光/国家/',光)。然后(功能(响应){
            的console.log('标题:',response.headers);
            的console.log('状态:',response.status);
            的console.log('配置:',response.config);
            的console.log('数据:',response.data);
            self.state = response.data;
        },功能(errResponse){
          console.error('在更新光状态错误);
        })
        的console.log('用户点击提交用:,light.id);
        的console.log('响应:',self.light);
    }

在服务器端,我有以下的方法应该响应请求:

  @POST
 @Path(/州/)
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.TEXT_HTML)
 公共字符串setStateOfLight(JSONObject的下车){
    如果(下车== NULL){
      返回这是一个空;
    }其他{
      的System.out.println(内容:+下车);
    }
    的JSONObject的JSONObject =新的JSONObject();
    //更新光的状态,给定id
    对于(光灯:lightCollection){
        // .....
    }
    返回getLightsStateAsJSON();
 }

当我点击我的按钮,我得到以下信息:

据火狐我做了我的请求发送JSON。至少它是这么说的,当我检查发送数据。

这是我的头请求数据:

我是什么在这里失踪?


解决方案

  

我下载了2.17新泽西州所有的依赖关系。至少,它说在他们的下载网站


呀等等泽西分布(新泽西州JAX-RS 2.0 RI束)不来用JSON转换支持捆绑在一起,除了基本的低水平类型,可以从的InputStream 转换。因此,没有其他任何进来的罐子旁边泽西捆绑,你必须使用的唯一一种是字符串的InputStream 字节[] 。没有真正的帮助不大,如果你试图操纵JSON

如何转换的工作原理是通过使用化MessageBodyReader 中和 MessageBodyWriter 秒。我不知道是什么的JSONObject 是的,但我猜测它的此处。在任何情况下,你需要一个化MessageBodyReader 为它处理传入的转换。我的猜测是,你还没有一个。我个人不知道从哪里得到一个特定的API。

我反而利用,可以处理JSON来POJO映射库。杰克逊是一个我会推​​荐。基本都是pretty容易理解。之所以这样说,是你的JSON

  {名:你的名字,ID:2}

所有你需要做的就是创建一个类的字段的特性,使用 JavaBean的命名约定 。因此,对于上面的JSON,我们可以利用这个

 公共类用户{
    私人字符串名称;
    私人字符串ID;    公共字符串的getName(){返回名称; }
    公共无效setname可以(字符串名称){this.name =名称; }    公共字符串的getId(){返回ID; }
    公共无效SETID(字符串ID){this.id = ID; }
}

现在你的方法参数可以接受用户键入。

要获得这种支持,你需要添加杰克逊提供商。下载下面


  • 新泽西媒体JSON -jackson


  • 获取所有一个从下面的图片

    我的另一篇文章与v2.2.3-不顾版本有一个图像。该版本你想获得2.3.2所有。它们都可以在上述链接中找到,对于第一依赖性。只需在搜索栏中搜索。当你找到它,选择版本并下载。


添加这些罐子后。你应该有JSON到POJO的支持。

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?

解决方案

"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.

这篇关于JSON发送到服务器是给415不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆