9

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.jackson.date-format'속성을 사용하는 방법?

그러나 스프링 부트 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') 
} 

나는 다음과 같은 수 0

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입니까?

답변

3

해당 속성은 java.util.Date serialization에만 사용되고 java.time.* 클래스에는 사용되지 않습니다.

당신은 @JsonFormat(pattern="dd.MM.yyyy")

또는

더 합리적인 형식을 사용 java.time.* 클래스의 ObjectMapper에 org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer 인터페이스 및 설정 사용자 지정 시리얼 라이저를 구현하는 bean을 만들기로 지정된 형식 (필드 당) 수요에 미칠 수있는 .

+0

좋아요. 하지만 java.util.Date (또는 java.sql.Date) 만 사용하는 경우 어떻게 사용합니까? – MiguelMunoz