-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customize the Jackson2ObjectMapperBuilder instead of replacing it #3838
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ var util = require('util'), | |
_ = require('lodash'), | ||
prompts = require('./prompts'), | ||
scriptBase = require('../generator-base'), | ||
cleanup = require('../cleanup'), | ||
packagejs = require('../../package.json'), | ||
crypto = require('crypto'), | ||
mkdirp = require('mkdirp'); | ||
|
@@ -343,6 +344,10 @@ module.exports = JhipsterServerGenerator.extend({ | |
|
||
writing: { | ||
|
||
cleanupOldServerFiles: function() { | ||
cleanup.cleanupOldServerFiles(this, this.javaDir, this.testDir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cleanup called by app/index.js cannot be used because this.javaDir is null. |
||
}, | ||
|
||
writeGlobalFiles: function () { | ||
this.template('_README.md', 'README.md', this, {}); | ||
this.copy('gitignore', '.gitignore'); | ||
|
@@ -672,8 +677,6 @@ module.exports = JhipsterServerGenerator.extend({ | |
this.template(SERVER_MAIN_SRC_DIR + 'package/domain/_package-info.java', javaDir + 'domain/package-info.java', this, {}); | ||
|
||
this.template(SERVER_MAIN_SRC_DIR + 'package/domain/util/_JSR310DateConverters.java', javaDir + 'domain/util/JSR310DateConverters.java', this, {}); | ||
this.template(SERVER_MAIN_SRC_DIR + 'package/domain/util/_JSR310DateTimeSerializer.java', javaDir + 'domain/util/JSR310DateTimeSerializer.java', this, {}); | ||
this.template(SERVER_MAIN_SRC_DIR + 'package/domain/util/_JSR310LocalDateDeserializer.java', javaDir + 'domain/util/JSR310LocalDateDeserializer.java', this, {}); | ||
if (this.databaseType === 'sql') { | ||
this.template(SERVER_MAIN_SRC_DIR + 'package/domain/util/_JSR310PersistenceConverters.java', javaDir + 'domain/util/JSR310PersistenceConverters.java', this, {}); | ||
this.template(SERVER_MAIN_SRC_DIR + 'package/domain/util/_FixedH2Dialect.java', javaDir + 'domain/util/FixedH2Dialect.java', this, {}); | ||
|
52 changes: 37 additions & 15 deletions
52
generators/server/templates/src/main/java/package/config/_JacksonConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,49 @@ | ||
package <%=packageName%>.config; | ||
|
||
import <%=packageName%>.domain.util.*; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
import java.time.*; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; | ||
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer; | ||
|
||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
@Configuration | ||
public class JacksonConfiguration { | ||
|
||
public static final DateTimeFormatter ISO_FIXED_FORMAT = | ||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.of("Z")); | ||
|
||
public static final DateTimeFormatter ISO_DATE_OPTIONAL_TIME = | ||
new DateTimeFormatterBuilder() | ||
.append(DateTimeFormatter.ISO_LOCAL_DATE) | ||
.optionalStart() | ||
.appendLiteral('T') | ||
.append(DateTimeFormatter.ISO_TIME) | ||
.toFormatter(); | ||
|
||
@Autowired | ||
private Jackson2ObjectMapperBuilder builder; | ||
|
||
//To be replaced by a Jackson2ObjectMapperBuilderCustomizer in Spring-boot 1.4 | ||
@PostConstruct | ||
public void postConstruct() { | ||
this.builder.serializers(new ZonedDateTimeSerializer(ISO_FIXED_FORMAT)); | ||
//Will not be needed anymore with SB 1.4 (Jackson > 2.7.1) | ||
this.builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(ISO_DATE_OPTIONAL_TIME)); | ||
} | ||
|
||
@Bean | ||
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { | ||
JavaTimeModule module = new JavaTimeModule(); | ||
module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); | ||
module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); | ||
module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); | ||
module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); | ||
module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); | ||
return new Jackson2ObjectMapperBuilder() | ||
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
.findModulesViaServiceLoader(true) | ||
.modulesToInstall(module); | ||
public ObjectMapper jacksonObjectMapper() { | ||
return this.builder.createXmlMapper(false).build(); | ||
} | ||
|
||
} |
25 changes: 0 additions & 25 deletions
25
generators/server/templates/src/main/java/package/domain/util/_JSR310DateTimeSerializer.java
This file was deleted.
Oops, something went wrong.
64 changes: 0 additions & 64 deletions
64
...tors/server/templates/src/main/java/package/domain/util/_JSR310LocalDateDeserializer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to use
generator.fs.delete()
but there is currently an issue (yeoman/generator#950) so we can see about that later.