Skip to content
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 3 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions generators/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const constants = require('./generator-constants'),
ANGULAR_DIR = constants.ANGULAR_DIR;

module.exports = {
cleanupOldFiles: cleanupOldFiles
cleanupOldFiles: cleanupOldFiles,
cleanupOldServerFiles: cleanupOldServerFiles
};
/**
* Removes files that where generated in previous JHipster versions and therefore needs to be removed
* Removes files that where generated in previous JHipster versions and therefore need to be removed
*/
function cleanupOldFiles(generator, javaDir, testDir) {
if (generator.isJhipsterVersionLessThan('3.1.1')) {
Expand All @@ -16,3 +17,13 @@ function cleanupOldFiles(generator, javaDir, testDir) {
generator.removeFile(ANGULAR_DIR + 'components/form/uib-pagination.config.js');
}
}

/**
* Removes server files that where generated in previous JHipster versions and therefore need to be removed
*/
function cleanupOldServerFiles(generator, javaDir, testDir) {
if (generator.isJhipsterVersionLessThan('3.4.3')) {
generator.removeFile(javaDir + 'domain/util/JSR310DateTimeSerializer.java');
Copy link
Member Author

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.

generator.removeFile(javaDir + 'domain/util/JSR310LocalDateDeserializer.java');
}
}
7 changes: 5 additions & 2 deletions generators/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -343,6 +344,10 @@ module.exports = JhipsterServerGenerator.extend({

writing: {

cleanupOldServerFiles: function() {
cleanup.cleanupOldServerFiles(this, this.javaDir, this.testDir);
Copy link
Member Author

@cbornet cbornet Jul 18, 2016

Choose a reason for hiding this comment

The 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');
Expand Down Expand Up @@ -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, {});
Expand Down
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();
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ spring:
enabled: true
livereload:
enabled: false # we use gulp + BrowserSync for livereload
jackson:
serialization.indent_output: true
<%_ if (databaseType == 'sql') { _%>
datasource:
<%_ if (devDatabaseType == 'mysql') { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ spring:
# Otherwise, it will be filled in by <%= buildTool %> when building the WAR file
# Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
active: #spring.profiles.active#
jackson:
serialization.write_dates_as_timestamps: false
<%_ if (databaseType == 'sql') { _%>
jpa:
open-in-view: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package <%=packageName%>.web.rest;

import <%=packageName%>.domain.util.JSR310DateTimeSerializer;
import <%=packageName%>.domain.util.JSR310LocalDateDeserializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.http.MediaType;

import java.io.IOException;
import java.nio.charset.Charset;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;

import static <%=packageName%>.config.JacksonConfiguration.ISO_DATE_OPTIONAL_TIME;
import static <%=packageName%>.config.JacksonConfiguration.ISO_FIXED_FORMAT;

/**
* Utility class for testing REST controllers.
*/
Expand All @@ -39,11 +39,8 @@ public static byte[] convertObjectToJsonBytes(Object object)
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

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);
module.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(ISO_FIXED_FORMAT));
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(ISO_DATE_OPTIONAL_TIME));
mapper.registerModule(module);

return mapper.writeValueAsBytes(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration
<%_ } _%>
jackson:
serialization.write_dates_as_timestamps: false
<%_ if (databaseType == 'sql') { _%>
datasource:
url: jdbc:h2:mem:<%= baseName %>;DB_CLOSE_DELAY=-1
Expand Down
2 changes: 0 additions & 2 deletions test/test-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ const expectedFiles = {
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/domain/PersistentToken.java',
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/domain/User.java',
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/domain/util/JSR310DateConverters.java',
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/domain/util/JSR310DateTimeSerializer.java',
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/domain/util/JSR310LocalDateDeserializer.java',
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/domain/util/JSR310PersistenceConverters.java',
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/repository/package-info.java',
SERVER_MAIN_SRC_DIR + 'com/mycompany/myapp/repository/AuthorityRepository.java',
Expand Down