在全球对象映射器中注册模块



我的主题类有一个数据成员duration类型LocalTime

    @Entity
    @Table(name= "topics")
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
    property  = "id",
    scope     = Long.class)
    @DynamicInsert(true)
    @DynamicUpdate(true)
    public class Topics implements Serializable {
        private static final long serialVersionUID = -1777454701749518940L;
        @Id
        @Column(name= "id")
        private Long id = Long.parseLong(UUID.randomUUID().toString().substring(0, 8), 16);
        @NotEmpty
        @NotNull
        @Column(name= "topic", columnDefinition = "Text", length = 50000)
        private String topic;
        @Convert(converter = LocalTimeConverter.class)
        private LocalTime duration;
        // getters and setters
     }

输入JSON:

{
    "topic":"x111",
    "imageUrl":"http://x.com/x1/x11/x111",
    "duration":"00:00:05"
}

错误:

Can not construct instance of java.time.LocalTime: no String-argument constructor/factory method to deserialize from String value ('00:00:05')

解决方案:

POM.XML
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.fasterxml.jackson</groupId>
                <artifactId>jackson-bom</artifactId>
                <version>2.9.0</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
   <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-parameter-names</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>

ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
or
mapper.registerModule(new ParameterNamesModule())
      .registerModule(new Jdk8Module())
      .registerModule(new JavaTimeModule());

它工作正常,但是我该如何在全球注册模块。

我通过扩展WebConfigurationAdapter

尝试
@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.registerModule(new ParameterNamesModule())
                    .registerModule(new Jdk8Module())
                    .registerModule(new JavaTimeModule());
        //objectMapper.findAndRegisterModules();
        converter.setObjectMapper(objectMapper);
        converters.add(converter);
        super.configureMessageConverters(converters);
    }

但没有起作用

只需将此代码放在您的主春季启动应用程序类

  @Autowired
  void configureObjectMapper(final ObjectMapper mapper) {
   mapper.registerModule(new ParameterNamesModule())
  .registerModule(new Jdk8Module())
  .registerModule(new JavaTimeModule());
  }

应该解决这个问题。
您甚至可以使用@Bean创建功能。
而不是使用 new创建对象拍摄者。您需要的类中的ObjectMapper自动射击。

相关内容

  • 没有找到相关文章

最新更新