在 springboot

时间:2021-02-03 14:54:52

标签: spring-boot spring-mvc date-format localdate localtime

我有一个模型,它由 springboot 应用程序中的 2 个不同端点返回,具有不同的数据。 下面是描述我的问题的简化代码。

class myModel{

    private LocalDate effectiveDate;
    private LocalTime effectiveTime;
    private LocalDate createDate;
    private LocalTime createdTime;
}

@RestController
class myController{

@GetMapping("firstEndpoint")
ResponseEntity<MyModel> getDateInFirstFormat(){
   MyModel model = callService();
   return new ResponseEntity<>(model, HttpStatus.OK);
}

@GetMapping("secondEndPint")
ResponseEntity<MyModel> getDateInFirstFormat(){
   MyModel model = callService();
   return new ResponseEntity<>(model, HttpStatus.OK);
}

直到现在,我的应用程序中只有 firstEndPoint,所以我在我的应用程序中创建了一个 Bean 到 Marshal,Demarshal LocalDate,

@Configuration
class configclass{ 

 @Bean
    public Formatter<LocalDate> localDateFormatter() {
        return new Formatter<LocalDate>() {
            @Override
            public LocalDate parse(String text, Locale locale) throws ParseException {
                return LocalDate.parse(text, DateTimeFormatter.ISO_DATE);
            }

            @Override
            public String print(LocalDate object, Locale locale) {
                return DateTimeFormatter.ISO_DATE.format(object);
            }
        };
    }
}

但现在我添加了 secondEndPoint,我需要以不同的格式发送日期 (MMM. dd. yyyy)

如何在两个端点的同一模型上有选择地应用不同的格式?

0 个答案:

没有答案
相关问题