简体   繁体   English

REST-将带有额外未定义参数的JSON请求映射到Java对象时,服务器抛出异常

[英]REST - Server Throwing Exception when Mapping JSON Request with Extra Undefined Parameters to Java Object

I'm using a RESTFUL J2EE Web Server; 我正在使用RESTFUL J2EE Web服务器; when my method in a resource class is consuming JSON and a there is a JSON request with extra parameters which are not defined in my java mapping object, the server throws exception. 当我的资源类中的方法使用JSON并且有JSON请求带有未在Java映射对象中定义的额外参数时,服务器将引发异常。
The problem is: I DON'T WANT THE SERVER TO THROW EXCEPTION! 问题是: 我不想服务器抛出异常! - I just want the server to ignore the request and return an error code like '400 Bad Request'! -我只希望服务器忽略该请求并返回错误代码,例如“ 400 Bad Request”!

My method: 我的方法:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createUser(final UserCreateRequest request) {
    return request.getHandler().handle();
}

My Simplified Java Class: 我的简化Java类:

public final class UserCreateRequest extends BaseRequest {
    @JsonProperty("username")
    private String username;

    @JsonProperty("password")
    private String password;

    public final String getUsername() {
        return username;
    }

    public final void setUsername(String username) {
        this.username = username.trim().toLowerCase();
    }

    public final String getPassword() {
        return password;
    }

    public final void setPassword(String password) {
        this.password = password.trim();
    }

    @Override
    @JsonIgnore
    public BaseRequestHandler getHandler() {
        return new UserCreateHandler(this);
    }
}

Sample wrong JSON request: 示例错误的JSON请求:

{
     username: "test",
     password: "1234",
     name: "hello"
}

In this sample the parameter 'name' is undefined! 在此示例中,参数“名称”未定义!

If I were you, I will not return a 400 bad request. 如果我是你,我将不会返回400错误的请求。 Rather, I will just ignore that extra field that comes in as a part of the json. 相反,我只是忽略了作为json一部分出现的额外字段。 I find it a lot more graceful of a way to not return an error just because some extra field(s) are sent as a part of the request (although, that is dependent on who implements it). 我发现,不仅仅因为一些额外的字段作为请求的一部分发送(尽管这取决于谁实施此错误),就更不会出现错误,这非常合适。

Now coming back to you real question, the problem is HOW the conversion happens from JSON to your UserCreateRequest . 现在回到您真正的问题,问题是如何从JSON转换为UserCreateRequest It seems like you are using the built-in deserializer of JAX-RS (or some other built-in deserializer) which does not understand what to do when having extra fields in the json object. 似乎您正在使用JAX-RS的内置反序列化器(或其他内置的反序列化器),当在json对象中包含额外字段时,它不理解该怎么办。 The way I normally handle this is using GSON library ( gson ) and convert the json string that comes in, manually. 我通常的处理方式是使用GSON库( gson )并手动转换输入的json字符串。 GSON library is by default capable of handling/ignoring extra fields that come in as a part of JSON String. 默认情况下,GSON库能够处理/忽略作为JSON字符串一部分出现的额外字段。

In terms of code, here's how I would handle it: 在代码方面,这是我的处理方式:

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createUser(String request) {
    Gson gson = new Gson();
    UserCreateRequest user = gson.fromJson(request, UserCreateRequest.class);
    return user.getHandler().handle();
}

The above code will let gson handle the conversion of JSON string to UserCreateRequest object, ignoring the extra fields. 上面的代码将让gson处理JSON字符串到UserCreateRequest对象的转换,而忽略了额外的字段。 That being said, GSON library is highly customizable in terms of how you want to handle serialization/deserialization of JSON strings (look at GSON's exclusion and inclusion strategies). 话虽这么说,GSON库在如何处理JSON字符串的序列化/反序列化方面具有高度可定制性(请参阅GSON的排除和包含策略)。

I have found this to be the easiest way of handling serialization/deserialization of JSON strings after playing around with couple of other (such as Jackson) libraries. 我发现这是在处理其他几个(例如Jackson)库之后处理JSON字符串的序列化/反序列化的最简单方法。 I hope you do too! 我希望你也这样做!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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