Ajax请求春REST API 415错误 [英] Ajax request to Spring REST api 415 error

查看:890
本文介绍了Ajax请求春REST API 415错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的春节休息控制器出现问题。
我尝试后( PUT )从我的客户数据( angularJS )到我的服务器(),但每次我试图发送时间这是我收到了不支持415媒体错误。

I have a problem with my Spring Rest controller. I am trying to post (PUT) data from my client (angularJS) to my server (Spring), but every time I try to send something I get a 415 Media not supported error.

使用Maven的我已经加入杰克逊芯(2.6.3)杰克逊 - 数据绑定(2.6.3)我的春天API。我也使用 @EnableWebMvc 来自动杰克逊的消息转换器加到春天。在我的春节,控制器我使用 @RestController 访问春天的REST方法。

With Maven I have added jackson-core (2.6.3) and jackson-databind (2.6.3) to my Spring API. I am also using @EnableWebMvc to automatically add the Jackson message converter to Spring. In my Spring controller I am using @RestController for access to the REST methods of Spring.

我的REST API的控制器:

@RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public RippleUser updateUserLocation(@PathVariable("id") Integer id, @RequestBody RippleUser user) {
    return user;
}

我曾尝试differend类型消耗的:

I have tried differend types of consumes:


  • MediaType.APPLICATION_JSON_VALUE

  • 应用/ JSON

  • 无消耗

  • 等等

我的RippleUser模型 (部分)

@Entity
@Table(name = "user")
@JsonRootName(value = "user")
public class RippleUser implements Serializable {

    @NotNull
    @Column(name = "active", nullable = false)
    private boolean activeUser;

    @Column(name = "latitude", nullable = true)
    private Float lattitude;

    @Column(name = "longitude", nullable = true)
    private Float longitude;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "lastActive", nullable = true)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy HH:mm:ss")
    private Date timestamp;
}

在这个模型中我拥有所有必要的getter和setter。

In this model I have all the necessary getters and setters.

我的AngularJS客户端:

 httpService.updateInBackend = function (url, data, callback, errorCallback) {
                http.put(url, data)
                        .then(function successCallback(response) {
                            callback(response.data);
                        }, function errorCallback(response) {
                            errorCallback(response);
                        });
            };

的网址:的http://服务器:端口/应用/位置/更新/ {ID}

数据:

params: {
    {
        "user": {
            "latitude": 52.899370,
            "longitude": 5.804548,
            "timestamp": 1449052628407
        }
    }
};

对于这种方法,我还添加了 @JsonRootName(值=用户)我RippleUser模式。

For this method I also added @JsonRootName(value = "user") to my RippleUser model.

我也试图未经用户属性(也来自我的模型删除它):

I have also tried without the user attribute (also removed it from my model):

params: {
    {
        "latitude": 52.899370,
        "longitude": 5.804548,
        "timestamp": 1449052628407
    }
};

该AngularJS HTTP方法(PUT,POST,DELETE,GET等)的检查是什么类型的参数,并自动设置正确的头。

The AngularJS HTTP method (PUT, POST, DELETE, GET, and so on) checks what type is in the parameters and automatically sets the right headers.

镀铬邮差

只是为了确保我自己也尝试这种方法在 Chrome的邮差插件

Just to make sure I have also tried this method in the Chrome Postman plugin

的网址:的http://服务器:端口/应用/位置/更新/ 2

方法: PUT

标题:内容类型:应用程序/ JSON

正文:

{
    "latitude": 52.899370,
    "longitude": 5.804548,
    "timestamp": 1449052628407
}


这给了错误是:


The error this gives is:

HTTP Status 415 The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.


更新

我可以接收信息,在我的RestController当我改变从RippleUser我要@ResponseBody字符串:

I can receive information in my RestController when I change my @ResponseBody from RippleUser to String:

@RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public RippleUser updateUserLocation(@PathVariable("id") Integer id, @RequestBody String user) {
    return user;
}

我也尝试发送一个空的用户对象,但会导致同样的错误消息。

I also tried sending an empty user object, but that results in the same error message

回答我的问题如下。

Answer to my problem is below.

推荐答案

本code的问题是比我预想的有点不同。 415媒体不支持错误是由春抛出,因为它无法在所有的岗位填写必填字段。

The problem with this code is a bit different than I expected. The 415 media not supported error is thrown by Spring, because it could not fill in all the required fields with the post.

我的解决办法是增加特别是使用的春天来转换数据和从同时发送和接收的类。这个额外的类的好处是,我可以管理该API发送和接收的数据。

My solution was to add a class especially used by Spring to convert data to and from while sending and receiving. The benefit of this extra class is that I can manage the data the API sends and receives.

UserLocation

添加下面的类到我的项目并获得成功(与getter和setter):

Adding the following class to my project did the trick (with getters and setters):

public class UserLocation {

    private Float latitude;

    private Float longitude;

    private Date lastActive;
}

本类包含了所有我的PUT请求必要的数据。弹簧自动填写所有字段。

This class contains all the necessary data for my PUT request. Spring automatically fills in all the fields.

控制器

为了使这项工作我也不得不修改我的控制器。新的code是如下:

To make this work I also had to modify my controller. The new code is as follows:

@RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT)
public UserLocation updateUserLocation(@PathVariable("id") Integer id, @RequestBody UserLocation location) {
    //Find user by ID
    //Add location to user
    //Update the user

    return //the new user
}


希望这有助于任何人

这篇关于Ajax请求春REST API 415错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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