相关文章推荐
耍酷的跑步鞋  ·  ios ...·  2 月前    · 
直爽的棒棒糖  ·  C++ ...·  1 年前    · 
伤情的充电器  ·  vs2008 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
  @Bean
   public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
            return new Jackson2JsonMessageConverter();

Event class:

public class TheEvent{
    @JsonFormat(pattern = "dd::MM::yyyy")
    private  LocalDateTime date;
//setters getters

When I send it, it arrives as:

{"month":"JULY","year":2018,"dayOfMonth":12,"dayOfWeek":"THURSDAY","dayOfYear":193,"hour":16,"minute":29,"nano":835000000,"second":24,"monthValue":7,"chronology":{"id":"ISO","calendarType":"iso8601"}},"direction":1}"

How can I serialize this date object in predefined pattern? (Remember that I just registering bean Jackson2JsonMessageConverter)

Also tried this:

@Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {      
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
        return objectMapper;
                Inject the pre-created ObjectMapper into the Jackson2JsonMessageConverter else you get one without the Java8 date time module .
– M. Deinum
                Jul 12, 2018 at 13:55
                Setting SerializationFeature.WRITE_DATES_AS_TIMESTAMPS to true will write them as timestamps (seconds in epoch). Also do you have the proper dependencies? Which boot/spring version are you using? And which LocalDateTime the JDK or JodaTime one?
– M. Deinum
                Jul 13, 2018 at 5:50
@Bean
public Jackson2JsonMessageConverter converter(Jackson2ObjectMapperBuilder builder) {
    ObjectMapper objectMapper = builder.createXmlMapper(false).build();
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
    return new Jackson2JsonMessageConverter(objectMapper);

In project I used RabbitListener and that Jackson2JsonMessageConverter

Listener:

@RabbitListener(queues = "${spring.rabbitmq.queue}")
@Transactional
public void receiveSocialPost(SocialPost socialPost) {
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.