java - 无效定义异常 : No serializer found for inner class

标签 java json rest spring-boot jackson

我有一个类,我想以 JSON 格式返回到 RestAPI 调用

return ResponseEntity.ok().cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES))
                    .body(hotelChart2);

类(class):

public class HotelChart2 {



    public HotelChart2() {
        super();
    }

    public class Statistics {

         double min;
         double max;
         double average;

         public Statistics() {
            super();
         }

        public Statistics(double min, double max, double average) {
            super();
            this.min = min;
            this.max = max;
            this.average = average;
        }

    }

    Map<LocalDateTime, DoubleSummaryStatistics> las24HPerHour;

    Map<LocalDate, Statistics> last30DPerDay;

    Map<LocalDate, Statistics> last3MPerDay;

    Map<LocalDate, Statistics> last6MPerDay;

    Map<LocalDate, Statistics> last1YPerDay;



    public Map<LocalDateTime, DoubleSummaryStatistics> getLas24HPerHour() {
        return las24HPerHour;
    }

    public void setLas24HPerHour(Map<LocalDateTime, DoubleSummaryStatistics> las24hPerHour) {
        las24HPerHour = las24hPerHour;
    }

    public Map<LocalDate, Statistics> getLast30DPerDay() {
        return last30DPerDay;
    }

    public void setLast30DPerDay(Map<LocalDate, Statistics> last30dPerDay) {
        last30DPerDay = last30dPerDay;
    }

    public Map<LocalDate, Statistics> getLast3MPerDay() {
        return last3MPerDay;
    }

    public void setLast3MPerDay(Map<LocalDate, Statistics> last3mPerDay) {
        last3MPerDay = last3mPerDay;
    }

    public Map<LocalDate, Statistics> getLast6MPerDay() {
        return last6MPerDay;
    }

    public void setLast6MPerDay(Map<LocalDate, Statistics> last6mPerDay) {
        last6MPerDay = last6mPerDay;
    }




    public Map<LocalDate, Statistics> getLast1YPerDay() {
        return last1YPerDay;
    }

    public void setLast1YPerDay(Map<LocalDate, Statistics> last1yPerDay) {
        last1YPerDay = last1yPerDay;
    }


}

但是我收到了这个错误:

Type definition error: [simple type, class com.tdk.api.json.HotelChart2$Statistics]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.tdk.api.json.HotelChart2$Statistics and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.tdk.api.json.HotelChart2["last3MPerDay"]->java.util.HashMap["2019-01-27"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:293)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)

最佳答案

这两种解决方案都有效!

公开所有成员(默认情况下,Jackson 在公共(public)成员字段上工作)

 public double min; //note: members are made public
 public double max;
 public double average;
 public Map<LocalDateTime, DoubleSummaryStatistics> las24HPerHour;
 // ..... modify all map as public

如果要修改 Jackson 默认属性,请修改 ObjectMapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(mapper.getVisibilityChecker()
             .withFieldVisibility(JsonAutoDetect.Visibility.ANY));

关于java - 无效定义异常 : No serializer found for inner class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149147/

相关文章:

Javascript - JSON - 嵌套和分组

java - 在 JSON 中包含文本文件

javascript - 将内容类型设置为 application/json 时出现意外 token 错误

java - 具有泄漏上下文的静态字段

java - Kafka-Connect 添加 SQL JAR 文件到类路径

java - Apache Samza 不运行

c# - 使用 LINQ 将列表投影到键/计数二维数组

java - 如何检测我的 JSON 字符串是否在 Java 中转义了 HTML 标签

java - 使用 Spring Boot 向 REST API 获取/发布请求

java - 如何使用Spark数据帧将csv数据加载到配置单元中?