java – Ajax请求Spring REST api 415错误

我的
Spring Rest控制器有问题.

我试图从我的客户端(angularJS)发布(PUT)数据到我的服务器(Spring),但每次我尝试发送一些东西时,我得到415 Media不支持错误.

使用Maven,我在我的Spring API中添加了jackson-core(2.6.3)和jackson-databind(2.6.3).我也使用@EnableWebMvc自动将Jackson消息转换器添加到Spring.在我的Spring控制器中,我使用@RestController来访问Spring的REST方法.

我的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;
}

我尝试了不同类型的消费:

> MediaType.APPLICATION_JSON_VALUE
>“application / 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.

我的AngularJS客户端:

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

URL:http:// server:port / app / location / update / {id}

数据:

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

对于这个方法,我还将@JsonRootName(value =“user”)添加到我的RippleUser模型中.

我也试过没有user属性(也从我的模型中删除它):

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

AngularJS HTTP方法(PUT,POST,DELETE,GET等)检查参数中的类型并自动设置正确的标头.

Chrome Postman

只是为了确保我在Chrome Postman插件中也尝试过这种方法

URL:http:// server:port / app / location / update / 2

方法:PUT

标题:Content-Type:application / json

身体:

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

这给出的错误是:

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.

更新

当我将@ResponseBody从RippleUser更改为String时,我可以在RestController中接收信息:

@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;
}

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

我的问题的答案如下.

最佳答案 这段代码的问题与我的预期有点不同. Spring抛出了不支持415媒体的错误,因为它无法用帖子填写所有必填字段.

我的解决方案是添加一个特别是由Spring用来在发送和接收时转换数据的类.这个额外类的好处是我可以管理API发送和接收的数据.

用户位置

将以下类添加到我的项目中就可以了(使用getter和setter):

public class UserLocation {

    private Float latitude;

    private Float longitude;

    private Date lastActive;
}

该类包含我的PUT请求的所有必要数据. Spring会自动填充所有字段.

调节器

为了完成这项工作,我还必须修改我的控制器.新代码如下:

@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
}

希望这有助于任何人

点赞