jackson 无法反序列化包含从AMQP消息传送接收的LocalDate的对象

c9qzyr3d  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(169)

我尝试通过AMQP消息传递(RabbitMQ)发送一个对象,但是当我收到消息时,转换器引发了JsonMappingException,因为它无法反序列化该消息。

订单.java:

  1. import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
  2. import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  3. import com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer;
  4. import com.fasterxml.jackson.datatype.joda.ser.LocalDateSerializer;
  5. import org.joda.time.LocalDate;
  6. import javax.persistence.*;
  7. import java.util.List;
  8. /**
  9. * Created by Flaviu Cicio on 07.07.2016.
  10. */
  11. @Entity
  12. @Table(name = "t_order", schema = "orders")
  13. public class Order {
  14. @Id
  15. private Long id;
  16. private Long userId;
  17. @JsonSerialize(using = LocalDateSerializer.class)
  18. @JsonDeserialize(using = LocalDateDeserializer.class)
  19. private LocalDate date;
  20. @OneToMany(mappedBy="byOrder", cascade = CascadeType.ALL)
  21. @GeneratedValue( strategy = GenerationType.IDENTITY )
  22. private List<OrderProduct> products;
  23. public Order() {
  24. }
  25. public Order(Long id, Long userId, List<OrderProduct> products, LocalDate date) {
  26. this.id = id;
  27. this.userId = userId;
  28. this.products = products;
  29. this.date = date;
  30. }
  31. public Long getId() {
  32. return id;
  33. }
  34. public void setId(Long id) {
  35. this.id = id;
  36. }
  37. public Long getUserId() {
  38. return userId;
  39. }
  40. public void setUserId(Long userId) {
  41. this.userId = userId;
  42. }
  43. public List<OrderProduct> getProducts() {
  44. return products;
  45. }
  46. public void setProducts(List<OrderProduct> products) {
  47. this.products = products;
  48. }
  49. public LocalDate getDate() {
  50. return date;
  51. }
  52. public void setDate(LocalDate date) {
  53. this.date = date;
  54. }
  55. @Override
  56. public String toString() {
  57. return "Order{" +
  58. "id=" + id +
  59. ", userId=" + userId +
  60. ", date=" + date +
  61. '}';
  62. }
  63. }

兔子配置.java:

  1. import org.springframework.amqp.core.AmqpAdmin;
  2. import org.springframework.amqp.core.Queue;
  3. import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  4. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  5. import org.springframework.amqp.rabbit.core.RabbitAdmin;
  6. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  7. import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
  8. import org.springframework.amqp.support.converter.MessageConverter;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. @Configuration
  12. public class RabbitConfiguration {
  13. @Bean
  14. public ConnectionFactory connectionFactory() {
  15. CachingConnectionFactory connectionFactory =
  16. new CachingConnectionFactory("localhost");
  17. return connectionFactory;
  18. }
  19. @Bean
  20. public AmqpAdmin amqpAdmin() {
  21. return new RabbitAdmin(connectionFactory());
  22. }
  23. @Bean
  24. public MessageConverter jsonMessageConverter(){
  25. return new Jackson2JsonMessageConverter();
  26. }
  27. @Bean
  28. public RabbitTemplate rabbitTemplate() {
  29. RabbitTemplate template = new RabbitTemplate(connectionFactory());
  30. template.setRoutingKey("order-queue");
  31. template.setMessageConverter(jsonMessageConverter());
  32. return template;
  33. }
  34. @Bean
  35. public Queue userQueue() {
  36. return new Queue("order-queue", false);
  37. }
  38. }

订单控制器.java:

  1. import com.orderservice.service.OrderService;
  2. import com.shopcommon.model.Order;
  3. import com.shopcommon.model.OrderProduct;
  4. import com.shopcommon.model.Product;
  5. import com.shopcommon.model.User;
  6. import org.apache.log4j.Logger;
  7. import org.springframework.amqp.core.AmqpAdmin;
  8. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import java.io.IOException;
  12. import java.net.HttpURLConnection;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. @RestController
  18. @RequestMapping(value = "/orders")
  19. public class OrderController {
  20. Logger logger = Logger.getLogger(OrderController.class);
  21. @Autowired
  22. OrderService orderService;
  23. @Autowired
  24. RabbitTemplate rabbitTemplate;
  25. @Autowired
  26. AmqpAdmin rabbitAdmin;
  27. @RequestMapping(method = RequestMethod.GET)
  28. public List<Order> getOrders(){
  29. return orderService.get();
  30. }
  31. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  32. public Order getOrder(@PathVariable("id") Long id){
  33. logger.info("Returned order: " + orderService.getById(id));
  34. return orderService.getById(id);
  35. }
  36. /**
  37. * Persist a new order received from amqp messaging
  38. *
  39. * @return
  40. */
  41. @RequestMapping(method = RequestMethod.POST)
  42. public Order createOrder(){
  43. Order order = (Order) rabbitTemplate.receiveAndConvert("order-queue");
  44. logger.info("Created a new order: " + order);
  45. return orderService.create(order);
  46. }
  47. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  48. public void deleteOrder(@PathVariable("id") Long id){
  49. orderService.delete(id);
  50. }
  51. @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  52. public Order updateOrder(@PathVariable("id") Long id, @RequestBody Order order){
  53. List<OrderProduct> products = order.getProducts();
  54. products.stream().forEach(orderProduct -> orderProduct.setOrder(order));
  55. order.setProducts(products);
  56. return orderService.update(order);
  57. }
  58. /**
  59. * Get user from a specific order
  60. *
  61. * @param id
  62. * @return
  63. */
  64. @RequestMapping(value = "/{id}/user", method = RequestMethod.GET)
  65. public User getOrderUser(@PathVariable("id") Long id){
  66. Order order = orderService.getById(id);
  67. rabbitAdmin.purgeQueue("user-queue", false);
  68. try {
  69. logger.info("Requested from user-service user from order with id " + id);
  70. logger.info("USER ID " + order.getUserId());
  71. URL obj = new URL("http://localhost:9999/user-service/users/" + order.getUserId());
  72. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  73. con.getResponseCode();
  74. User user = (User)rabbitTemplate.receiveAndConvert("user-queue");
  75. logger.info("Received from user-service: " + user);
  76. return user;
  77. } catch (IOException e) {
  78. logger.error(e.getMessage());
  79. }
  80. logger.info("No user found!");
  81. return null;
  82. }
  83. /**
  84. * Get products from a specific order
  85. *
  86. * @param id
  87. * @return
  88. */
  89. @SuppressWarnings("Duplicates")
  90. @RequestMapping(value = "/{id}/products", method = RequestMethod.GET)
  91. public List<Product> getOrderProducts(@PathVariable("id") Long id){
  92. rabbitAdmin.purgeQueue("product-queue", false);
  93. List<Product> products = new ArrayList<>();
  94. logger.info("Requested from product-service products from order with id " + id);
  95. Order order = orderService.getById(id);
  96. order.getProducts().forEach(orderProduct -> {
  97. try {
  98. URL obj = new URL("http://localhost:9999/product-service/products/" + orderProduct.getProductId());
  99. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  100. con.getResponseCode();
  101. products.add((Product) rabbitTemplate.receiveAndConvert("product-queue"));
  102. } catch (MalformedURLException e) {
  103. logger.error(e.getMessage());
  104. } catch (IOException e) {
  105. logger.error(e.getMessage());
  106. }
  107. });
  108. logger.info("Received from product-service a number of " + products.size() + " products");
  109. return products;
  110. }
  111. }

堆栈追踪:

  1. 2016-07-13 09:40:27.668 ERROR 5108 --- [nio-8004-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.amqp.support.converter.MessageConversionException: Failed to convert Message content] with root cause
  2. com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected START_ARRAY: expected String, Number or JSON Array
  3. at [Source: {"id":null,"userId":1,"products":[],"date":{"dayOfMonth":13,"dayOfWeek":"WEDNESDAY","dayOfYear":195,"month":"JULY","monthValue":7,"year":2016,"hour":9,"minute":40,"nano":199000000,"second":27,"chronology":{"id":"ISO","calendarType":"iso8601"}}}; line: 1, column: 44] (through reference chain: com.shopcommon.model.Order["date"])
  4. at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261) ~[jackson-databind-2.8.0.jar:2.8.0]
  5. at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1340) ~[jackson-databind-2.8.0.jar:2.8.0]
  6. at com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:65) ~[jackson-datatype-joda-2.6.7.jar:2.6.7]
  7. at com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:15) ~[jackson-datatype-joda-2.6.7.jar:2.6.7]
  8. at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:490) ~[jackson-databind-2.8.0.jar:2.8.0]
  9. at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95) ~[jackson-databind-2.8.0.jar:2.8.0]
  10. at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276) ~[jackson-databind-2.8.0.jar:2.8.0]
  11. at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140) ~[jackson-databind-2.8.0.jar:2.8.0]
  12. at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789) ~[jackson-databind-2.8.0.jar:2.8.0]
  13. at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2871) ~[jackson-databind-2.8.0.jar:2.8.0]
  14. at org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.convertBytesToObject(Jackson2JsonMessageConverter.java:122) ~[spring-amqp-1.5.6.RELEASE.jar:na]
  15. at org.springframework.amqp.support.converter.Jackson2JsonMessageConverter.fromMessage(Jackson2JsonMessageConverter.java:92) ~[spring-amqp-1.5.6.RELEASE.jar:na]
  16. at org.springframework.amqp.rabbit.core.RabbitTemplate.receiveAndConvert(RabbitTemplate.java:819) ~[spring-rabbit-1.5.6.RELEASE.jar:na]
  17. at com.orderservice.controller.OrderController.createOrder(OrderController.java:62) ~[classes/:na]
  18. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
  19. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91]
  20. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91]
  21. at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91]
  22. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  23. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  24. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  25. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  26. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  27. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  28. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  29. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  30. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  31. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  32. at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  33. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  34. at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  35. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  36. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  37. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.36.jar:8.0.36]
  38. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  39. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  40. at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:281) ~[spring-boot-actuator-1.3.6.RELEASE.jar:1.3.6.RELEASE]
  41. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  42. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  43. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  44. at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:115) ~[spring-boot-actuator-1.3.6.RELEASE.jar:1.3.6.RELEASE]
  45. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  46. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  47. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  48. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  49. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  50. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  51. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  52. at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  53. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  54. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  55. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  56. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  57. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  58. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  59. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  60. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  61. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  62. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  63. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  64. at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:103) ~[spring-boot-actuator-1.3.6.RELEASE.jar:1.3.6.RELEASE]
  65. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  66. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  67. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  68. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.36.jar:8.0.36]
  69. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.36.jar:8.0.36]
  70. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.36.jar:8.0.36]
  71. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.36.jar:8.0.36]
  72. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.36.jar:8.0.36]
  73. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.36.jar:8.0.36]
  74. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528) [tomcat-embed-core-8.0.36.jar:8.0.36]
  75. at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099) [tomcat-embed-core-8.0.36.jar:8.0.36]
  76. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670) [tomcat-embed-core-8.0.36.jar:8.0.36]
  77. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520) [tomcat-embed-core-8.0.36.jar:8.0.36]
  78. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476) [tomcat-embed-core-8.0.36.jar:8.0.36]
  79. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_91]
  80. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_91]
  81. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.36.jar:8.0.36]
  82. at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
yvt65v4c

yvt65v4c1#

首先,您应该添加以下maven依赖项:

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.datatype</groupId>
  3. <artifactId>jackson-datatype-jsr310</artifactId>
  4. </dependency>

其次,您应该使用以下代码更新您的MessasgeConverter bean:

  1. @Bean
  2. public MessageConverter messageConverter()
  3. {
  4. ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
  5. return new Jackson2JsonMessageConverter(mapper);
  6. }

在此更新之后,您不需要使用“JsonSerialize”注解。

展开查看全部
piv4azn7

piv4azn72#

对于任何还在寻找答案的人,他们可以在他们的应用程序中复制粘贴:)
下面的代码要求spring注入其默认的对象Map器,然后要求它注册类路径中的所有模块。因此,请确保您在pom或gradle文件中包含了“com.fasterxml.jackson.datatype:jackson-datatype-jsr 310”。

  1. @Configuration
  2. @Slf4j
  3. public class SpringInternalObjectMapperConfig {
  4. /*
  5. Java 8 date/time type `java.time.LocalDateTime` not supported by default:
  6. add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
  7. */
  8. @Bean
  9. public Jackson2JsonMessageConverter messageConverter(ObjectMapper springInternalObjectMapper) {
  10. log.info("Updating spring's object mapper.");
  11. springInternalObjectMapper.findAndRegisterModules();
  12. return new Jackson2JsonMessageConverter(springInternalObjectMapper);
  13. }
  14. }
展开查看全部

相关问题