简体   繁体   English

JSON验证其余服务

[英]JSON Verification rest service

Enviorment : Java, Jersey with Jackson, Tomcat. 环境:Java,Jersey和Jersey,Tomcat。

I have a rest service taking JSON input from the client. 我有一个休息服务,从客户端获取JSON输入。 I want to verify the input, if its JSON or not and then it JSON then the key in the JSON input is as expected or not. 我想验证输入(如果是否为JSON,然后为JSON,那么JSON输入中的键是否为预期值)。 If yes then it should produce HTTP 400-Bad request. 如果是,则应产生HTTP 400错误请求。

For example- 例如-

// REST SERVICE
    @POST
    @Path("login")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response login(Credentials login,
            @Context HttpServletRequest httpRequest) {


        if (login == null)
            // return sendBadRequest(HTTP 400);

        String userName = (String) login.getUsername();
        String passWord = (String) login.getPassword();

        if (userName == null)
            // return sendBadRequest(HTTP 400);
        else if (passWord == null)
            // return sendBadRequest(HTTP 400);


    .....
}

// Credentials.java
@JsonIgnoreProperties(ignoreUnknown=true) 
public class Credentials {

    private String username;

    private String password;

// Getters and setters...
}

For example - non json input like 例如-非json输入

{"key" : "val"
should produce HTTP 400. As well as input with non proper values like 应该产生HTTP400。以及具有非正确值的输入,例如
 {"usernames" : "abc", "passwords" : "abc"} {“用户名”:“ abc”,“密码”:“ abc”} 
should produce HTTP 400. 应该产生HTTP 400。

My code works well for case 2 mentioned but I expected it to work for case 1 as well. 我的代码适用于上述情况2,但我希望它也适用于情况1。 when non-json input is there, I expected it to set the Credential object to null and then I can the null object in 'if' to return HTTP 400. But it was not the case. 当存在非json输入时,我希望它可以将Credential对象设置为null,然后可以在“ if”中使用null对象返回HTTP400。但是事实并非如此。 Is there anything that rest framework provides for this case ? rest框架可以为这种情况提供什么吗?

After a long research I managed to find the answer for this. 经过长时间的研究,我设法找到了答案。 We have to use @Provider in rest framework to handle this error. 我们必须在rest框架中使用@Provider来处理此错误。

Here is an example for this : 这是一个例子:

@Provider
public class JsonParseExceptionHandler implements
        ExceptionMapper {

    public Response toResponse(JsonParseException exception) {
        ResponseStatus status = new ResponseStatus();
        ClientResponse clientResponse = new ClientResponse();

        status.setCode(Status.BAD_REQUEST.getStatusCode());
        status.setMessage(Status.BAD_REQUEST.name());
        status.setFieldName("JSON Parsing Exception");
        status.setFieldDescription(exception.getMessage());

        clientResponse.setStatus(status);

        return Response
                .status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())
                .entity(clientResponse).build();
    }

}

You can use gson to convert you JSON into java object then you can validate 您可以使用gson将JSON转换为Java对象,然后可以进行验证

https://code.google.com/p/google-gson/ https://code.google.com/p/google-gson/

Or you can use Json Lib 或者您可以使用Json Lib

http://json-lib.sourceforge.net/index.html http://json-lib.sourceforge.net/index.html

The following method will help 以下方法会有所帮助

http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/util/JSONUtils.html#mayBeJSON(java.lang.String) http://json-lib.sourceforge.net/apidocs/jdk15/net/sf/json/util/JSONUtils.html#mayBeJSON(java.lang.String)

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

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