全局注册对象映射器中的模块

问题描述 投票:0回答:1

我有一个主题类有一个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);
    }

但它没有奏效

java spring-boot objectmapper fasterxml
1个回答
2
投票

只需将这段代码放在Main Spring Boot Application类中

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

这应该够了吧。 你甚至可以用@Bean创建一个函数。 而不是使用new创建ObjectMapper。在您需要的类中自动装配ObjectMapper。

© www.soinside.com 2019 - 2024. All rights reserved.