Home >Java >javaTutorial >How SpringBoot uses jackson to format time
Create the project and add dependencies
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
Create the entity class UserDTO
Add attributes and omit the get and set methods.
private String id; private String username; private Date createTime;
Create UserController
Write control layer code
@RestController public class UserController { @GetMapping("/getUser") public List<userdto> getUser() { List<userdto> userList = new ArrayList<userdto>(); for (int i=1; i<p>Calling interface: http://localhost:8080/getUser</p> <p><img src="https://img.php.cn/upload/article/000/887/227/168501804652134.png" alt="How SpringBoot uses jackson to format time"></p> <p>This result is obviously not what we need, so we need to format the time. And there is a time zone issue, my current time is 22:44 pm. </p> <h4>The first method is to use annotations<br> </h4> <p>Add the @JsonFormat annotation on the fields that need to be converted. This annotation is from jackson and is integrated with the web package. </p> <pre class="brush:php;toolbar:false">import com.fasterxml.jackson.annotation.JsonFormat; private String id; private String username; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date createTime;
pattern: The format of the time and date that needs to be converted
timezone: The time is set to the East Eighth District to avoid time errors in the conversion
Calling interface: http:/ /localhost:8080/getUser
is completed, but this also has disadvantages. If I have a hundred entities that all have Date types, then they must be in one Hundreds of entities are annotated. Seems a bit troublesome.
All json generation is inseparable from the related HttpMessageConverters
SpringBoot uses jackson by default and configures it by default . So let's modify it.
Global search for JacksonHttpMessageConvertersConfiguration. Idea shortcut key: Ctrl shift r
There is a method mappingJackson2HttpMessageConverter in this class that is used to process json.
@Bean @ConditionalOnMissingBean( value = {MappingJackson2HttpMessageConverter.class}, ignoredType = {"org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter", "org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter"} ) MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) { return new MappingJackson2HttpMessageConverter(objectMapper); }
Note that there are two annotations on this method, and the @Bean annotation will not be introduced. Introduce the ConditionalOnMissingBean annotation.
@ConditionalOnMissingBean: When the given bean does not exist, instantiate the current Bean.
An analogy: When you report for work, your company will see that you have brought a computer and will let you use your own computer. If you do not bring a computer, it will let you use the company's computer. SpringBoot does the same thing. If you don't provide it, it will use the default one.
New MyConfig
import java.text.SimpleDateFormat; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.fasterxml.jackson.databind.ObjectMapper; @Configuration public class MyConfig { @Bean MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverterConfiguration() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper om = new ObjectMapper(); //全局修改josn时间格式 om.setDateFormat(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")); converter.setObjectMapper(om); return converter; } }
Provides a Bean of MappingJackson2HttpMessageConverter, so Springboot will use what we provide.
Annotate the annotation of the User entity
Call the interface: http://localhost:8080/getUser
OK, this method is also possible.
Provide ObjectMapper
You can also provide an ObjectMapper and comment out the MappingJackson2HttpMessageConverter provided above.
import java.text.SimpleDateFormat; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.fasterxml.jackson.databind.ObjectMapper; @Bean ObjectMapper objectMapper() { ObjectMapper om = new ObjectMapper(); om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); return om; }
Calling interface: http://localhost:8080/getUser
Note: The above two methods are globally modified!
Modify the default configuration in application.yml or properties
yml
spring: jackson: date-format: yyyy/MM/dd timezone: GMT+8
properties
spring.jackson.date-format=yyyy-MM-dd HH:mm spring.jackson.time-zone=GMT+8
If the second method and the third method of configuration exist at the same time, the second method will prevail.
If all three methods exist, the annotation format in the entity class will be the main one.
The above is the detailed content of How SpringBoot uses jackson to format time. For more information, please follow other related articles on the PHP Chinese website!