【发布时间】: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<String, Object> payload需要有效的 json...您将如何将错误输入映射到地图上?但是尝试将其更改为简单的String payload,无论如何您都在验证字符串。 -
@sodik 可能会把它写成答案?
-
您可能遇到了未捕获的异常 HttpMessageNotReadableException。在您的 try-catch 中处理它并返回您选择的响应
-
当我尝试使用字符串有效负载时不起作用:gist.github.com/anonymous/dec3874feb853d644fec
标签: java json spring-mvc jackson spring-boot