2017-03-11 51 views
1

我學會了如何一起走過這個帖子使用JSON和java.util.Optional: https://medium.com/@harittweets/how-to-use-java-util-optional-and-json-together-12434f9d55b9#.r0kpo65f8@JsonFormat上的可選屬性

但我有另外一個問題,當類的屬性是Date類型,它必須是格式化。所以我用@JsonFormat:

public class UserBean extends BasicBean { 

    private String username; 

    private String password; 

    @JsonFormat(pattern = "yyyy-MM-dd") 
    private Optional<Date> dob; 

    // getter and setter 
} 

我使用Spring啓動我的應用程序,這是配置:

@SpringBootApplication 
@EnableAutoConfiguration 
@ComponentScan("com.hag.prj") 
public class Application{ 

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


@Configuration 
public class ApplicationConfig { 

    @Bean 
    @Primary 
    public ObjectMapper objectMapper() { 
     return Jackson2ObjectMapperBuilder.json().serializationInclusion(JsonInclude.Include.NON_NULL) 
       .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) 
       .modules(this.jdk8Module(), this.jsrModule(), this.parameterNamesModule()).build(); 
    } 

    @Bean 
    public Module jdk8Module() { 
     final Jdk8Module jdk8Module = new Jdk8Module(); 
     jdk8Module.configureAbsentsAsNulls(true); 
     return jdk8Module; 
    } 

    @Bean 
    public Module jsrModule() { 
     return new JavaTimeModule(); 
    } 

    @Bean 
    public Module parameterNamesModule() { 
     return new ParameterNamesModule(); 
    } 
} 

@RestController 
@RequestMapping("/user") 
public class UserController { 

    @RequestMapping(method = RequestMethod.POST) 
    public ResponseEntity<UserBean> create(UserBean userBean){ 
     return ResponseEntity.ok(userBean); 
    } 

} 

我使用高級REST客戶端測試,配置如下: enter image description here

結果是這樣的:

{ 
    timestamp: "2017-03-12T23:22:20.237+0000" 
    status: 400 
    error: "Bad Request" 
    exception: "org.springframework.validation.BindException" 
    errors: [1] 
     0: { 
     codes: [4] 
      0: "typeMismatch.userBean.dob" 
      1: "typeMismatch.dob" 
      2: "typeMismatch.java.util.Optional" 
      3: "typeMismatch" 
     arguments: [1] 
      0: { 
       codes: [2] 
        0: "userBean.dob" 
        1: "dob" 
       defaultMessage: "dob" 
       code: "dob" 
      } 
     defaultMessage: "Failed to convert property value of type 'java.lang.String' to required type 'java.util.Optional' for property 'dob'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value '1988-10-07'; nested exception is java.lang.IllegalArgumentException" 
     objectName: "userBean" 
     field: "dob" 
     rejectedValue: "1988-10-07" 
     bindingFailure: true 
     code: "typeMismatch" 
     } 
    message: "Validation failed for object='userBean'. Error count: 1" 
    path: "/user" 
} 

何我W能解決這個問題

+0

你有這個類路徑? (maven deps)https://github.com/FasterXML/jackson-modules-java8 – Eugene

+0

@Eugene是的,我有 – gianglaodai

+0

好吧...它適用於其他optionals然後呢? 'java.util.Date'以外的其他類型 – Eugene

回答

0

嘗試用:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")