【问题标题】:Trying to Validate JSON using Jackson through Spring Boot Rest尝试通过 Spring Boot Rest 使用 Jackson 验证 JSON
【发布时间】:2015-06-03 02:51:27
【问题描述】:

我正在尝试使用 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

我收到以下到标准输出:

{"name":"value"}
true

但是当对无效的 JSON 使用下面的 curl 命令时(故意去掉了右花括号):

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

我在标准输出中收到以下内容:

{"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"

有没有办法确保在服务器端处理异常但不在标准输出中抛出,然后让我的代码响应:

false

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

【问题讨论】:

  • Map&lt;String, Object&gt; payload 需要有效的 json...您将如何将错误输入映射到地图上?但是尝试将其更改为简单的String payload,无论如何您都在验证字符串。
  • @sodik 可能会把它写成答案?
  • 您可能遇到了未捕获的异常 HttpMessageNotReadableException。在您的 try-catch 中处理它并返回您选择的响应
  • 当我尝试使用字符串有效负载时不起作用:gist.github.com/anonymous/dec3874feb853d644fec

标签: java json spring-mvc jackson spring-boot


【解决方案1】:

我的猜测是问题出在控制器的 @RequestBody Map&lt;String, Object&gt; payload 声明上。 Spring MVC 需要将请求正文转换为Map,因此如果请求不包含正确的 JSON,则会失败。

但是,既然您想接受任何输入,请改用 @RequestBody String payload,作为奖励,您可以摆脱 GSON 到字符串的转换 :)

【讨论】:

【解决方案2】:

想通了!

添加了以下更改:

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

验证响应:

public class ValidationResponse {

    public String message;

    public String getMessage() {
        return message;
    }

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

对内容类型使用“文本/纯文本”:

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

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

【讨论】:

    【解决方案3】:

    在 spring 3.2 之后,您可以使用 org.springframework.web.bind.annotation.ControllerAdvice 来处理这些全局抛出的异常。 read more

    示例代码

    @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 格式无效错误,将执行此方法。您可以自定义您的响应。

    【讨论】:

      【解决方案4】:

      您的错误是您希望以Map 接收无效的JSON,您必须为此使用HttpEntity&lt;String&gt;,但这违背了您的休息控制器的全部目的。

      @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&lt;String, Object&gt;
      3. 考虑使用javax.validation.constraints,而不是直接检查
      4. 使用@RestControllerAdvice@ExceptionHandler 处理无效输入

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-26
        • 2015-06-01
        • 1970-01-01
        • 2020-07-25
        • 1970-01-01
        • 1970-01-01
        • 2016-12-01
        • 2022-01-14
        相关资源
        最近更新 更多