- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个返回 net.sf.json.JSONObject 的@RestController:
@PostMapping("/list")
public JSONObject listStuff(HttpServletRequest inRequest, HttpServletResponse inResponse) {
JSONObject json = new JSONObject();
...
return json;
}
当JSONObject包含空引用时,抛出如下异常:
Could not write JSON: Object is null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Object is null (through reference chain: net.sf.json.JSONObject[\"list\"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject[\"object\"]->net.sf.json.JSONNull[\"empty\"])"
这是我们现在正在清理的遗留代码,在某个时候我们将摆脱显式 JSON 操作,但这将是一个巨大的变化,现在我只想摆脱异常。我尝试了以下解决方案:
Include.NON_NULL
- 所以我的 WebMvcConfigurationSupportClass
中的这段代码:@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
ObjectMapper webObjectMapper = objectMapper.copy();
webObjectMapper.setSerializationInclusion(Include.NON_NULL);
converters.add(new MappingJackson2HttpMessageConverter(webObjectMapper));
}
spring.jackson.default-property-inclusion=non_null
com.fasterxml.jackson
的版本 - 在依赖树中找到的唯一版本是 2.9.7。以上都没有帮助。
关于如何告诉 Spring 忽略 net.sf.json.JSONObjects 中的空值有什么建议吗?
最佳答案
Include.NON_NULL
不起作用,因为 JSONNull
表示 null
但它不是 null
per se。来自 documentation :
JSONNull is equivalent to the value that JavaScript calls null, whilst Java's null is equivalent to the value that JavaScript calls undefined.
这个对象是作为一个 Singleton
实现的,它有两个方法:isArray
和 isEmpty
其中 isEmpty
是有问题的因为抛出异常。下面的代码片段显示了它的实现:
public boolean isEmpty() {
throw new JSONException("Object is null");
}
最好的方法是为JSONNull
类型定义NullSerializer
。下面的示例显示了我们如何配置它:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
public class JsonApp {
public static void main(String[] args) throws Exception {
SimpleModule netSfJsonModule = new SimpleModule("net.sf.json");
netSfJsonModule.addSerializer(JSONNull.class, NullSerializer.instance);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.registerModule(netSfJsonModule);
JSONObject object = new JSONObject();
object.accumulate("object", JSONNull.getInstance());
JSONArray jsonArray = new JSONArray();
jsonArray.add(object);
JSONObject json = new JSONObject();
json.accumulate("list", jsonArray);
System.out.println(mapper.writeValueAsString(json));
}
}
以上代码打印:
{
"list" : [ {
"object" : null
} ]
}
另见:
关于json - Spring 启动 JsonMappingException : Object is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54744703/
我以为我有一个父类参数,它有2个子类 ComboParameter 和 IntegerParameter @JsonSubTypes({ @JsonSubTypes.Type(value =
运行以下行时: Request> requestMap = JsonUtils.fromJson(eventContext.getMessage().getPayloadAsString(), Req
我已经使用 jersey 在 java 中编写了一个 REST API。当我使用 XML 请求时,一切正常,但当我使用 JSON 请求时,我总是得到 JsonMappingException。 这些对
我使用spring RestTemplate并将json响应转换为pojo。但得到一个 JsonMappingException POJO @Root public class Account { @
我的此类带有一个标有 @JsonCreator 的构造函数,用于将 JSON 字符串反序列化到我的类 Meal 中: public class Meal { private int prote
我的 IDE 在 mapper.readValue 行给我错误 Unhandled Exception com.fasterxml.jackson.databind.JsonMappingExcept
我在 Java 中有一个通用类型的类。它应该在收到一些 json 后返回一个 T 类型的对象。我正在使用以下代码创建对象: ObjectMapper mapper = new ObjectMapper
我有一个用 Java (spring boot) 编写的 rest api,我的请求从请求 header 中获取一个 json 字符串(不要问为什么这样:),例如{flowerId: 123}在我的
我正在尝试向我的网页显示数据库数据。当 GET 请求到 @RequestMapping(value = "/api/binder") 时,我做了以下代码。 但是当获取请求到达此方法时,它会获取数据(我
我正在构建一个使用并返回 JSON 的 RESTful Web 服务。当我尝试通过服务层从数据库中获取 ESRBRating 对象时,我遇到了以下堆栈跟踪。但是,当我将 Spring Data JPA
我在 Camel Route 中遇到了异常 Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer
对传递给 ObjectMapper Json 解析器方法的字符串进行 null 和空检查时,出现异常。这是我的代码: private static final ObjectMapper OBJECT_
我正在使用 Retrofit 2 和 jackson 转换器下载并解析此 json `{ "message": "Action complete.", "successCode": 0,
我知道关于我会告诉你的同一个问题有很多问题,但还没有一个能解决我的问题。 我已经实现了一项服务,该服务必须通过延迟加载来访问对象属性。这是类的简化: @Entity public class Trea
我有一个类需要使用 Jackson 从 JSON 反序列化。类结构如下所示: public class A { public B b; } public class B { publi
我尝试反序列化包含空属性并具有 JsonMappingException 的对象。 我的工作: String actual = "{\"@class\" : \"PersonResponse\","
尝试反序列化 JSON 时遇到以下异常 No suitable constructor found for type [simple type, class MyObj$obj$Card]: can
我有一个 Web 应用程序,其中服务器以 JSON 格式返回任何结果。为了创建 JSON,我使用 codehaus Jackson ObjectWriter 版本 1.9.8。 我遇到的问题是映射中有
我在尝试放置“可选实体过滤”时遇到问题。我有一个抽象类,如下所示: // In your Pom org.glassfish.jersey.ext je
尝试使用 @JsonIdentityInfo jackson 注释时出错。当我尝试反序列化对象时,出现以下异常: Could not read JSON: Already had POJO for i
我是一名优秀的程序员,十分优秀!