Springboot框架下Date类型数据上传问题(重要)

探讨了SpringBoot中使用Jackjson框架解析Date类型数据时遇到的问题,包括无法正确解析特定格式的日期字符串以及解析后的日期偏移问题,并提供了详细的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.csdn.net/pp_fzp/article/details/79588374

问题场景:

使用Springboot框架搭建服务,实现如下需求, 服务端使用实体类接收客户端上传具有相同结构的json数据信息,其中实体类的属性字段中包含java.util.Date类型的属性字段。

问题描述:

1.由客户端上传的json数据中Date字段格式为“2016-08-15 17:00:00”,测试调用时报如下错误:

{
  "timestamp": 1521217111334,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
  "message": "JSON parse error: Can not deserialize value of type java.util.Date from String \"2016-08-15 17:00:00\": not a valid representation (error: Failed to parse Date value '2016-08-15 17:00:00': Can not parse date \"2016-08-15 17:00:00\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String \"2016-08-15 17:00:00\": not a valid representation (error: Failed to parse Date value '2016-08-15 17:00:00': Can not parse date \"2016-08-15 17:00:00\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))\n at [Source: java.io.PushbackInputStream@6317f037; line: 9, column: 11] (through reference chain: com.consmation.iwebmapserver.projectserver.model.BusGeologicalDrilling[\"fixdt\"])",
  "path": "/MobileServer/SaveGeologicalDrillingInfo"
}
该错误大意为:不能解析日期\“2016-08-15 17:00:00 \”:虽然它似乎符合格式的yyyy-MM-dd 'HH:mm:ss.SSS”

2.此时将上传的Date数据修改为 2016-08-15T16:00:00 ,程序虽不再报错误,但是通过跟踪程序实体类发现,该字段的值为:2016-08-16 1:00:00,不符合我们所上传的日期。

错误分析:

1. Springboot使用的默认json解析框架是jackjson框架

2. jackjson解析框架在解析实体类里面是date数据类型的数据时的默认格式是:UTC类型,即yyyy-MM-dd'T'HH:mm:ss.SSS 并且默认为+8时区,即时间基础上加8小时

解决方案:

1. 在实体Date类型的字段上使用@JsonFormat注解格式化日期 

@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
2. 通过下面方式取消timestamps形式
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
备注:

如果项目中使用json解析框架为fastjson框架,则可使用如下解决方法:
1. 在实体字段上使用@JsonFormat注解格式化日期
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")

--------------------- 
作者:北辰之北灬 
来源:CSDN 
原文:https://blog.csdn.net/pp_fzp/article/details/79588374 
版权声明:本文为博主原创文章,转载请附上博文链接!

<think>好的,我现在需要帮助用户用Spring Boot框架搭建一个毕节特色农产品网站的前后端。首先,我得明确用户的需求,他们可能需要一个具备基本CRUD功能的网站,包括前端展示和后端管理。接下来,我应该考虑技术栈的选择。后端使用Spring Boot,这是常见的Java框架,能够快速搭建RESTful API。数据库方面,可能用MySQL,配合JPA或者MyBatis进行数据操作。前端的话,用户可能希望用Thymeleaf模板引擎,因为Spring Boot集成方便,或者可能更倾向于前后端分离,使用Vue.js或React。不过问题中没有特别说明,所以先按Thymeleaf来考虑,这样前后端结合更紧密,适合毕设项目。 然后,项目的结构需要规划。通常Spring Boot项目会有Controller、Service、Repository层。实体类对应农产品,包含字段如id、名称、描述、价格、图片地址等。需要创建对应的实体类,并配置JPA进行数据库管理。接下来是Controller,处理HTTP请求,比如获取所有农产品、添加新产品、编辑和删除的接口。前端页面需要列出产品,并有表单提交新产品,同时管理员可以进行编辑和删除操作。数据库配置方面,用户需要在application.properties中设置数据源,包括URL、用户名、密码,以及JPA的配置,比如ddl-auto设为update,这样启动时会自动更新表结构。然后编写Repository接口,继承JpaRepository,这样基本的CRUD方法就不用自己实现了。 前端部分,使用Thymeleaf模板,创建HTML页面,比如product-list.html,里面用Thymeleaf的语法遍历产品列表,显示每个产品的信息。添加表单,提交到后端的/add或/update端点。可能需要处理图片上传,这里可以简化,先让用户输入图片URL,之后如果需要,再实现文件上传功能。 安全性方面,可能需要管理员登录,这时候可以集成SpringSecurity,但用户的问题中没有提到,所以可能在后续问题中补充。现在先完成基础功能。 测试方面,用户可能需要通过Postman测试API,或者直接通过前端表单提交数据。需要确保各个端点正常工作,数据库操作正确。 可能的错误点包括:依赖没有正确添加,比如Spring Web、Thymeleaf、JPA、MySQL驱动等;实体类的注解是否正确;Repository是否被正确扫描;前端页面的Thymeleaf语法是否正确,比如th:each循环是否遍历了正确的模型属性。 接下来,编写代码示例。首先创建Spring Boot项目,选择所需的依赖。然后创建Product实体类,带有@Id和@GeneratedValue来管理主键。Controller层使用@GetMapping和@PostMapping处理请求,Service层调用Repository的方法。前端页面用Thymeleaf渲染数据,表单提交到对应的端点。 在解释代码时,需要说明各层的作用,比如Controller处理请求,Service处理业务逻辑,Repository负责数据库交互。实体类对应数据库表,Thymeleaf模板渲染HTML。 可能的相关问题包括如何添加分页功能、如何实现图片上传、如何集成Spring Security进行权限控制,以及如何部署到云服务器等。这些都是常见的后续开发步骤,用户可能会感兴趣。</think>以下是使用SpringBoot框架开发毕节特色农产品网站的实现方案: 1. 后端实现(SpringBoot部分) ```java // 实体类 Product.java @Entity public class Product { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String description; private Double price; private String imageUrl; private String origin; // 产地 private Date harvestTime; // 采收时间 // getters/setters } // 数据访问层 ProductRepository.java public interface ProductRepository extends JpaRepository<Product, Long> { List<Product> findByOrigin(String origin); } // 服务层 ProductService.java @Service public class ProductService { @Autowired private ProductRepository productRepository; public List<Product> getAllProducts() { return productRepository.findAll(); } public Product createProduct(Product product) { return productRepository.save(product); } } // 控制层 ProductController.java @RestController @RequestMapping("/api/products") public class ProductController { @Autowired private ProductService productService; @GetMapping public ResponseEntity<List<Product>> getAllProducts() { return ResponseEntity.ok(productService.getAllProducts()); } @PostMapping public ResponseEntity<Product> createProduct(@RequestBody Product product) { return ResponseEntity.status(HttpStatus.CREATED) .body(productService.createProduct(product)); } } ``` 2. 前端实现(使用Thymeleaf模板引擎) ```html <!-- src/main/resources/templates/products.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>毕节特色农产品</title> </head> <body> <h1>毕节特色农产品列表</h1> <table> <tr th:each="product : ${products}"> <td th:text="${product.name}"></td> <td th:text="${product.description}"></td> <td th:text="${'¥' + product.price}"></td> <td><img th:src="${product.imageUrl}" width="100"></td> </tr> </table> </body> </html> ``` 3. 配置文件 application.properties ```properties spring.datasource.url=jdbc:mysql://localhost:3306/bijie_products?useSSL=false spring.datasource.username=root spring.datasource.password=yourpassword spring.jpa.hibernate.ddl-auto=update spring.thymeleaf.cache=false ``` 实现说明: 1. 实体类定义农产品核心属性 2. Repository接口提供数据访问方法 3. Service层处理业务逻辑 4. Controller提供REST API接口 5. Thymeleaf模板实现前端展示 6. MySQL数据库存储产品数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值