我有一个javax.json.JsonObject并希望根据JSON模式验证它.所以我找到了com.github.fge.json-schema-validator.但它只适用于com.fasterxml.jackson.databind.JsonNode.
有没有办法把我JsonObject变成一个JsonNode?
是的,我知道这个问题已经讨论过几次了,但是我没有设法解决我的问题。
因此,我使用org.springframework.web.client.RestTemplate从http请求中获取了JSONObject :
JSONObject j = RestTemplate.getForObject(url, JSONObject.class);
Run Code Online (Sandbox Code Playgroud)
但是我得到这个错误:
Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "uri" (Class org.json.JSONObject), not marked as ignorable
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6f526c5f; line: 2, column: 12] (through reference chain: org.json.JSONObject["uri"]); nested exception is org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "uri" (Class org.json.JSONObject), not marked as ignorable
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6f526c5f; line: 2, column: 12] (through reference chain: org.json.JSONObject["uri"])
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:181)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:173)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:517)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:472)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
Run Code Online (Sandbox Code Playgroud)
我想访问Rest-Api,并且Json对象可以具有不同的字段名称。我已经尝试过了@JsonIgnoreProperties(ignoreUnknown=true)。但这行不通...
如何将响应放入JSONObject?