【问题标题】:how to save only the minute and second hour in mysql annotation springboot @@ JsonFormat如何在mysql注解spring boot @@ Json格式中只保存分秒小时
【发布时间】:2021-05-05 04:07:07
【问题描述】:

我正在使用 springboot 和 spring-data 创建一个 crud,用户将它保存在一个变量“条目”中,我希望它只有小时:分钟:秒,我尝试使用 @JsonFormat (pattern = "HH: mm: ss" ) 但报错

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "19:47:11": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '19:47:11' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 19:47:11 of type java.time.format.Parsed; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "19:47:11": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '19:47:11' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 19:47:11 of type java.time.format.Parsedat [Source: (PushbackInputStream); line: 10, column: 20] (through reference chain: br.com.lucas.entity.Acao["entrada"])]

我怎样才能只节省一小时?

【问题讨论】:

  • 尝试使用LocalTime 而不是LocalDateTime
  • 如果你想在 MySQL 中从DATETIME 中提取“小时”,有几种方法(MID(),DATE_FORMAT(),...)。如果你不想在那里做,请删除标签`'[mysql]'。
  • 当我使用本地时间这个错误:“状态”:500,“错误”:“内部服务器错误”,“跟踪”:“org.springframework.dao.DataIntegrityViolationException:无法执行语句;SQL [n/a];嵌套异常是 ...

标签: java mysql spring spring-boot spring-data


【解决方案1】:

我们可以通过添加带有模式参数的JsonFormat 注释来更改响应类中的格式。标准 SimpleDateFormat 规则适用。

@JsonFormat(pattern = "HH:mm:ss")
private LocalTime localTime;

我尝试了一个简单的例子,我想和你分享。如果你尝试这个没有报错,但是你在你的模型中尝试报错,你需要检查数据库中的相关列是time,如果不是,请尝试。

让我们创建一个模型作为示例。

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalTime;

@Entity
public class Time {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JsonFormat(pattern = "HH:mm:ss")
    private LocalTime localTime;

    public Time() {
    }

    // getter/setter ..
}

让我们现在创建一个存储库。

import org.springframework.data.jpa.repository.JpaRepository;

public interface TimeRepository extends JpaRepository<Time, Long> {}

现在让我们创建一个控制器。

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TimeController {

    private final TimeRepository timeRepository;

    public TimeController(TimeRepository timeRepository) {
        this.timeRepository = timeRepository;
    }

    @PostMapping(value = {"/time"})
    public Time time(@RequestBody Time time) {
        timeRepository.save(time);
        return time;
    }
}

现在让我们向/time 路径发送一个Json 请求。

{
    "localTime": "02:30:00"
}

你会得到这样的结果。

{
    "id": 1,
    "localTime": "02:30:00"
}

我建议您阅读this article,在那里您可以找到更全面的解决方案。

【讨论】:

  • 您的示例非常好,正是我的 api 大声笑,我切换到 LocalTime,即使在执行测试时我收到错误“trace”:“org.springframework.dao.DataIntegrityViolationException:无法执行语句;SQL [n/a];嵌套异常是 org.hibernate.exception.DataException: 无法执行语句\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.... 非常感谢我会阅读它们的链接。在我看来,银行不接受这种格式的事情发生了
  • 这个例子有效,它只存储时间值。您是否将数据库中LocalTime 字段的类型更改为time
  • iswailyildiz 非常感谢,我没有意识到,当我做到了,谢谢大家
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2023-03-19
  • 2015-05-18
  • 2021-10-22
  • 2019-04-13
  • 2020-12-19
  • 2016-09-19
  • 2015-05-20
相关资源
最近更新 更多