YasharHND
YasharHND

Reputation: 123

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

I'm using a RESTFUL J2EE Web Server; 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.
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'!

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:

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:

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

In this sample the parameter 'name' is undefined!

Upvotes: 0

Views: 2011

Answers (1)

shahshi15
shahshi15

Reputation: 2987

If I were you, I will not return a 400 bad request. Rather, I will just ignore that extra field that comes in as a part of the 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. 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. The way I normally handle this is using GSON library (gson) and convert the json string that comes in, manually. GSON library is by default capable of handling/ignoring extra fields that come in as a part of JSON String.

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

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. I hope you do too!

Upvotes: 1

Related Questions