-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scaffold): support register converter automatically
- Loading branch information
Showing
7 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
15 changes: 14 additions & 1 deletion
15
...{artifactId}-provider/src/main/java/${basePath}/config/ApplicationConfiguration.tmpl.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
26 changes: 26 additions & 0 deletions
26
...${artifactId}-provider/src/main/java/${basePath}/config/web/ConverterConfigurer.tmpl.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ${basePackage}.config.web; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.format.FormatterRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import java.util.Map; | ||
|
||
@Configuration | ||
public class ConverterConfigurer implements WebMvcConfigurer { | ||
@Autowired | ||
private ApplicationContext context; | ||
|
||
@Override | ||
public void addFormatters(FormatterRegistry registry) { | ||
Map<String, Converter> beans = context.getBeansOfType(Converter.class); | ||
beans.forEach((k, v) -> { | ||
if (v != null) { | ||
registry.addConverter((Converter<?, ?>) v); | ||
} | ||
}); | ||
} | ||
} |
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
Empty file.
15 changes: 15 additions & 0 deletions
15
...src/main/java/${basePath}/converter/if_demo.String2DemoControllerModelConverter.tmpl.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ${basePackage}.converter; | ||
|
||
import ${basePackage}.controller.DemoController; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class String2DemoControllerModelConverter implements Converter<String, DemoController.Model> { | ||
@Override | ||
public DemoController.Model convert(String source) { | ||
DemoController.Model target = new DemoController.Model(); | ||
target.setContent(source); | ||
return target; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../templates/${artifactId}-provider/src/main/java/${basePath}/util/ConverterUtils.tmpl.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ${basePackage}.util; | ||
|
||
import org.springframework.core.convert.ConversionService; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public abstract class ConverterUtils { | ||
private static ConversionService conversionService; | ||
|
||
public static void setConversionService(ConversionService conversionService) { | ||
ConverterUtils.conversionService = conversionService; | ||
} | ||
|
||
public static <FROM, TO> TO convert(FROM from, Class<TO> toClazz) { | ||
return conversionService.convert(from, toClazz); | ||
} | ||
|
||
public static <FROM, TO> List<TO> convertAll(List<FROM> fromList, Class<TO> toCls) { | ||
if (fromList == null || fromList.size() == 0) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
return fromList | ||
.stream() | ||
.map(from -> conversionService.convert(from, toCls)).collect(Collectors.toList()); | ||
} | ||
} |
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