Skip to content

Commit

Permalink
feat(scaffold): support integration with gson
Browse files Browse the repository at this point in the history
  • Loading branch information
taccisum committed Oct 12, 2019
1 parent d1eea6c commit 1c9b622
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $ yo deepexi-spring-cloud --help
|APM|✅️SkyWalking|☑️Zipkin|☑️PinPoint|
|分库分表|☑️Sharding-JDBC|☑️MyCAT|
|服务器|✅Tomcat|☑️Jetty|☑️Undertow|
|JSON解析器|✅Jackson|✅️FastJson|️Gson|
|JSON解析器|✅Jackson|✅️FastJson|️Gson|
|模板引擎|✅Thymeleaf|☑️Freemarker|
|对象存储|☑️AliOSS|☑️FastDFS|
|ORM|✅MybatisPlus|✅Mapper|☑️JPA|
Expand Down
31 changes: 31 additions & 0 deletions generators/app/handler/configurer/gson_configurer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const _ = require('lodash');

module.exports = {
key: 'gson',
fn: {
configureProviderPomDependencies (optionalDependencies, props) {
optionalDependencies.push({
dependency: [
{ groupId: 'com.google.code.gson' },
{ artifactId: 'gson' }
]
})
},
configureApplicationYaml (yaml, env, props) {
switch (env) {
case 'default': {
_.merge(yaml, {
spring: {
http: {
converters: {
'preferred-json-mapper': 'gson'
}
}
}
});
break;
}
}
}
}
}
3 changes: 2 additions & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const obj = {
type: 'list',
choices: [
'jackson',
'fastjson'
'fastjson',
'gson'
],
message: '请选择你使用的JSON解析器'
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ${basePackage}.config.web;

import com.google.gson.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spring.web.json.Json;

import java.lang.reflect.Type;

@Configuration
public class GsonConfigurer implements WebMvcConfigurer {
@Bean
public GsonBuilder gsonBuilder() {
GsonBuilder builder = new GsonBuilder();
// for spring fox swagger json serialization
builder.registerTypeAdapter(Json.class, new JsonSerializer<Json>() {
private JsonParser parser = new JsonParser();

@Override
public JsonElement serialize(Json json, Type type, JsonSerializationContext jsonSerializationContext) {
return parser.parse(json.value());
}
});
return builder;
}
}
35 changes: 34 additions & 1 deletion generators/test/app/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ const expects = {
thymeleaf: new Expect(),
redis: new Expect(),
skywalking: new Expect(),
fastjson: new Expect()
fastjson: new Expect(),
gson: new Expect()
};

const required = expects.required;
Expand Down Expand Up @@ -628,6 +629,27 @@ fastjson.addProviderClasses([
'config/web/FastJsonConfigurer.java'
])

const gson = expects.gson;
gson.addProviderArtifacts([
'gson'
])
gson.addProviderClasses([
'config/web/GsonConfigurer.java'
])
gson.assertProperties = () => {
it('should have properties', () => {
assert.strictEqual(readYamlConfigs().spring.http.converters['preferred-json-mapper'], 'gson')
});
}
gson.assertNoProperties = () => {
it('should have properties', () => {
try {
assert.notStrictEqual(readYamlConfigs().spring.http.converters['preferred-json-mapper'], 'gson');
} catch (e) {
}
});
}

function assertByExpected (expected, expects) {
describe('required files or classes', () => {
for (const key in expects) {
Expand Down Expand Up @@ -831,5 +853,16 @@ describe('optional dependencies', () => {

assertByExpected(['required', 'demo', 'fastjson'], expects)
});

describe('gson', () => {
before(() => {
return generate({
jsonParser: 'gson',
demo: true
})
});

assertByExpected(['required', 'demo', 'gson'], expects)
});
});
});

0 comments on commit 1c9b622

Please sign in to comment.