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ブートの 'spring.jackson.date-format'プロパティの使い方

ただし、Springブート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として文書化されていませんか?

答えて

3

このプロパティは、java.util.Dateシリアル化でのみ使用され、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

関連する問題