https://github.com/rozidan/modelmapper-spring-boot-starter
A Spring Boot starter that let you use ModelMapper within your Spring Boot application
https://github.com/rozidan/modelmapper-spring-boot-starter
java modelmapper modelmapper-spring-boot-starter spring-boot
Last synced: about 1 month ago
JSON representation
A Spring Boot starter that let you use ModelMapper within your Spring Boot application
- Host: GitHub
- URL: https://github.com/rozidan/modelmapper-spring-boot-starter
- Owner: rozidan
- License: apache-2.0
- Created: 2017-08-25T19:20:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-07-27T12:55:38.000Z (over 6 years ago)
- Last Synced: 2024-12-22T19:03:00.762Z (about 1 year ago)
- Topics: java, modelmapper, modelmapper-spring-boot-starter, spring-boot
- Language: Java
- Homepage: http://modelmapper.org
- Size: 102 KB
- Stars: 20
- Watchers: 1
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Spring Boot ModelMapper Starter
> A Spring Boot starter that let you use [ModelMapper](http://modelmapper.org) within your Spring Boot application.
> Base on [jmnarloch modelmapper project](https://github.com/jmnarloch/modelmapper-spring-boot-starter) with Modelmapper newest version adjustments.
[](https://travis-ci.org/rozidan/modelmapper-spring-boot-starter)
[](https://coveralls.io/github/rozidan/modelmapper-spring-boot-starter?branch=master)
[](https://maven-badges.herokuapp.com/maven-central/com.github.rozidan/modelmapper-spring-boot-starter/)
[](https://oss.sonatype.org/content/repositories/snapshots/com/github/rozidan/modelmapper-spring-boot-starter/)
[](http://www.apache.org/licenses/LICENSE-2.0.html)
## Features
Register the ModelMapper to your Spring Boot application and allows to configure it and register object mappings.
## Setup
In order to add ModelMapper to your project simply add this dependency to your classpath:
```xml
com.github.rozidan
modelmapper-spring-boot-starter
2.3.1
```
```groovy
compile 'com.github.rozidan:modelmapper-spring-boot-starter:2.3.1'
```
For snapshots versions add the sonatype public repository:
```groovy
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/groups/public" }
...
}
```
### Change ModelMapper global configuration
In order to set change ModelMapper global configuration, simply register within Spring application context instance of `MapperConfigurer`:
```java
@Component
public class GlobalConfiguration extends MapperConfigurer {
@Override
public void configure(Configuration configuration) {
configuration.setSkipNullEnabled(true);
configuration.setMatchingStrategy(MatchingStrategies.STRICT);
}
}
```
### Overriding the default mapping
In order to override the default mapping of some object types, lets say User -> UserDto, simply register within Spring application context instance of `TypeMapConfigurer`:
```java
@Component
public class MapperConfigurer extends TypeMapConfigurer {
@Override
public void configure(TypeMap typeMap) {
typeMap.addMapping(User::getAge, UserDto::setAge);
typeMap.addMappings(mapping -> mapping.skip(UserDto::setMiddleName));
typeMap.setPreConverter(context -> {
String[] name = context.getSource().getName().split(" ");
context.getDestination().setFirstName(name[0]);
context.getDestination().setLastName(name[1]);
return context.getDestination();
});
}
}
```
### Custom converter
In order to register ModelMapper Converter, simply register within Spring application context instance of `ConverterConfigurer`:
```java
@Component
public class SomeEnumTypeConverter extends ConverterConfigurer {
@Override
public Converter converter() {
return new AbstractConverter() {
@Override
protected ProductCategoryDto convert(ProductCategory source) {
...
}
};
}
}
```
### Testing
Scan for mapping components with the `WithModelMapper` annotation
```java
@RunWith(SpringRunner.class)
public class MapperTest {
@Test
public void someTest() {
}
@Configuration
@WithModelMapper(basePackage = "com.company.program.mapping")
public static class Application {
}
}
```
## License
[Apache-2.0](http://www.apache.org/licenses/LICENSE-2.0)