ebean-orm / ebean-jackson

Jackson ObjectMapper module that uses Ebean's JSON support for serialisation and deserialisation
5 stars 2 forks source link

I'm using Spring MVC. How to customize the date format pattern #4

Open johntostring opened 9 years ago

johntostring commented 9 years ago

Didn't work like this way

public class StartupListener implements ServerConfigStartup {

    @Override
    public void onStart(ServerConfig serverConfig) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒"));
        serverConfig.setObjectMapper(objectMapper);
        //serverConfig.setJsonDateTime(JsonConfig.DateTime.ISO8601);
    }
}
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

    static {
        if (!AgentLoader.loadAgentFromClasspath(
                "avaje-ebeanorm-agent",
                "debug=1;packages=com.millinch.ebean.demo.domain")) {
            LOGGER.info("avaje-ebeanorm-agent not found in classpath - not dynamically loaded");
        }
    }

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

    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒"));
        objectMapper.registerModule(new JacksonEbeanModule());
        return objectMapper;
    }

    @Bean
    public MappingJackson2JsonView mappingJackson2JsonView() {
        MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
        jsonView.setObjectMapper(objectMapper());
        return jsonView;
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper());
        return converter;
    }
}
@RestController
public class UserApi {

    @RequestMapping(value = "/user")
    public List<User> getUsers() {
        return User.find.findList();
    }

}
rbygrave commented 8 years ago

Ok, I have dropped the ball on this one. Should get to it shortly.