尝试通过Spring Boot Rest使用Jackson验证JSON

5

我正在尝试使用Spring Boot创建一个RESTful Web服务,它将接收JSON并使用Jackson进行验证。

以下是RESTful Web服务代码:

import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.Gson;

@RestController
@RequestMapping("/myservice")
public class ValidationService {    

    @RequestMapping(value="/validate", method = RequestMethod.POST)
    public void validate(@RequestBody Map<String, Object> payload) throws Exception {
        Gson gson = new Gson();
        String json = gson.toJson(payload); 
        System.out.println(json);
        boolean retValue = false;

        try {
            retValue = Validator.isValid(json);
        } 
        catch(Throwable t) {
            t.printStackTrace();
        }
        System.out.println(retValue);

    }
}

以下是验证器的代码:
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Validator {

    public static boolean isValid(String json) {
        boolean retValue = false;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
            JsonParser parser = objectMapper.getFactory().createParser(json);
            while (parser.nextToken() != null) {}
            retValue = true;
            objectMapper.readTree(json);
        }catch(JsonParseException jpe) {
            jpe.printStackTrace();
        }
        catch(IOException ioe) {

        }
        return retValue;
    }
}

因此,当我使用curl发送有效的JSON时:

curl -H "Accept: application/json" -H "Content-type: application/json" \ 
-X POST -d '{"name":"value"}' http://localhost:8080/myservice/validate

我从stdout接收到以下内容:

{"name":"value"}
true

当我们使用以下curl命令解析无效的JSON(故意删除了右花括号):

curl -H "Accept: application/json" -H "Content-type: application/json" \
 -X POST -d '{"name":"value"' http://localhost:8080/myservice/validate

我收到以下stdout输出:

我在stdout中收到以下内容:

{"timestamp":1427698779063,
 "status":400,"error":
 "Bad Request",
 "exception":"org.springframework.http.converter.HttpMessageNotReadableException",
 "message":"Could not read JSON: 
 Unexpected end-of-input: expected close marker for OBJECT 
 (from [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 0])\n
 at [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 31]; 
 nested exception is
 com.fasterxml.jackson.core.JsonParseException: 
 Unexpected end-of-input: expected close marker for OBJECT 
 (from [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 0])\n 
 at [Source: java.io.PushbackInputStream@1edeb3e; line: 1, column: 31]",
 "path":"/myservice/validate"

有没有一种方法可以确保服务器端处理异常,而不是在stdout中抛出异常,然后只需让我的代码响应:

false

感谢您花时间阅读本文...

1
Map<String, Object> payload需要有效的JSON格式...如果输入有误,你会如何映射到map上?不过,试着将其改为简单的String payload,反正你已经在验证字符串了。 - sodik
@sodik 可以把那写成一个答案吗? - ci_
你可能会遇到未捕获的异常HttpMessageNotReadableException。在try-catch中处理它,并返回您选择的响应。 - Wand Maker
当我尝试使用字符串负载时,它无法工作:https://gist.github.com/anonymous/dec3874feb853d644fec - PacificNW_Lover
4个回答

1

我已经明白了!

添加了以下更改:

在 @RequestMapping 代码部分内:

consumes = "text/plain",
produces = "application/json"

将 @RequestBody 从 Map 更改为 String 负载。

ValidationService 类:

@RequestMapping(value="/validate", 
                method = RequestMethod.POST, 
                consumes="text/plain", 
                produces="application/json")
public ValidationResponse process(@RequestBody String payload) throws JsonParseException,
                                                                      IOException {
    ValidationResponse response = new ValidationResponse();
    boolean retValue = false;
    retValue = Validator.isValid(payload);
    System.out.println(retValue);
    if (retValue == false) {
        response.setMessage("Invalid JSON");
    }
    else {
        response.setMessage("Valid JSON");
    }
    return response;
}

验证器类:

import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Validator {

    public static boolean isValid(String json) {
        boolean retValue = true;
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
            JsonFactory factory = mapper.getFactory();
            JsonParser parser = factory.createParser(json);
            JsonNode jsonObj = mapper.readTree(parser);
            System.out.println(jsonObj.toString());
        }
        catch(JsonParseException jpe) {
            retValue = false;   
        }
        catch(IOException ioe) {
            retValue = false;
        }
        return retValue;
    }
}

ValidationResponse:

public class ValidationResponse {

    public String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

使用"text/plain"作为内容类型:
curl -H "Accept: application/json" -H "Content-type: text/plain" -X POST -d \ 
 '{"name":"value"}' http://localhost:8080/myservice/validate

现在,一切都正常!这太棒了!

0

在Spring 3.2之后,您可以使用org.springframework.web.bind.annotation.ControllerAdvice来处理这些全局抛出的异常。阅读更多

示例代码

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<?> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
        MultipleReadHttpRequest request) {

    Map<String, String> errorResponse = new HashMap<>();
    errorResponse.put("error", ex.getMessage());
    errorResponse.put("code", "01");

    return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}

如果出现JSON格式无效错误,将执行此方法。您可以自定义响应。

0

我猜问题出在你控制器中的@RequestBody Map<String, Object> payload声明上。Spring MVC需要将请求体转换为Map,因此如果请求不包含正确的JSON,则会失败。

但是,由于你想接受任何输入,请改用@RequestBody String payload,这样你还可以摆脱GSON转换为字符串的麻烦 :)


当我尝试使用字符串负载时,它无法工作:https://gist.github.com/anonymous/dec3874feb853d644fec - PacificNW_Lover
可能是因为您的 Content-type 是 application/json,而实际内容并不是 json。 - ci_

0
你的错误在于期望无效的 JSON 被接收为 Map,你需要使用 HttpEntity 来解决这个问题,但这背离了你的 REST controller 设计的初衷。
@RequestMapping(value = "/validate", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String validate(HttpEntity<String> request) {
    final String json = request.getBody();
  1. 避免同时使用Gson和Jackson,以及直接解析json
  2. 考虑使用专用类型代替Map<String,Object>
  3. 考虑使用javax.validation.constraints,而不是直接检查
  4. 使用@RestControllerAdvice@ExceptionHandler处理无效输入

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接