sourcetip

Spring Boot의 'spring.jackson.date-format' 속성을 사용하는 방법은 무엇입니까?

fileupload 2023. 6. 27. 22:29
반응형

Spring Boot의 'spring.jackson.date-format' 속성을 사용하는 방법은 무엇입니까?

Current SpringBoot Reference Guide(현재 스프링 부트 참조 가이드)에 따라 다음을 설정합니다.spring.jackson.date-format자산, 다음과 같은 기능을 수행합니다.Date format string or a fully-qualified date format class name. For instance 'yyyy-MM-dd HH:mm:ss'.

그러나 Spring Boot 1.5.3에서는 이러한 방식으로 작동하지 않습니다.

이 클래스부터 시연합니다.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;

@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

@RestController
class NowController{

  @GetMapping("/now")
  Instant getNow(){
    return Instant.now();
  }

}

그리고 이것src/main/resources/application.properties

spring.jackson.date-format=dd.MM.yyyy

그리고 이것build.gradle:

buildscript {
    ext {
        springBootVersion = '1.5.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('com.fasterxml.jackson.datatype:jackson-datatype-jdk8')
    compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

다음을 확인할 수 있습니다.

http localhost:8080/now
HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Date: Tue, 25 Apr 2017 22:37:58 GMT
Transfer-Encoding: chunked

"2017-04-25T22:37:58.319Z"

지정된 형식이 아닙니다.dd.MM.yyyy더 필요한 것이 있습니까?spring.jackson.date-format문서화된 대로 작동하지 않습니까?

해당 속성은 다음 용도로만 사용됩니다.java.util.Date직렬화 및 비연속화java.time.*반.

온디맨드(필드별) 형식을 지정할 수 있습니다.@JsonFormat(pattern="dd.MM.yyyy")

또는

다음을 구현하는 빈을 만듭니다.org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer인터페이스 및 ObjectMapper의 사용자 지정 직렬화 프로그램 설정java.time.*좀 더 합리적인 형식을 사용하는 수업

언급URL : https://stackoverflow.com/questions/43622259/how-to-use-spring-boots-spring-jackson-date-format-property

반응형