42 Bean Mapper Spring Support

Spring support for the Bean Mapper

License

License

GroupId

GroupId

io.beanmapper
ArtifactId

ArtifactId

beanmapper-spring
Last Version

Last Version

3.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

42 Bean Mapper Spring Support
Spring support for the Bean Mapper
Project Organization

Project Organization

42 BV
Source Code Management

Source Code Management

https://github.com/42BV/beanmapper-spring

Download beanmapper-spring

How to add to project

<!-- https://jarcasting.com/artifacts/io.beanmapper/beanmapper-spring/ -->
<dependency>
    <groupId>io.beanmapper</groupId>
    <artifactId>beanmapper-spring</artifactId>
    <version>3.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.beanmapper/beanmapper-spring/
implementation 'io.beanmapper:beanmapper-spring:3.1.0'
// https://jarcasting.com/artifacts/io.beanmapper/beanmapper-spring/
implementation ("io.beanmapper:beanmapper-spring:3.1.0")
'io.beanmapper:beanmapper-spring:jar:3.1.0'
<dependency org="io.beanmapper" name="beanmapper-spring" rev="3.1.0">
  <artifact name="beanmapper-spring" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.beanmapper', module='beanmapper-spring', version='3.1.0')
)
libraryDependencies += "io.beanmapper" % "beanmapper-spring" % "3.1.0"
[io.beanmapper/beanmapper-spring "3.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
io.beanmapper : beanmapper jar 3.0.1
com.google.guava : guava jar 30.1-jre
com.fasterxml.jackson.core : jackson-databind jar 2.12.0

provided (8)

Group / Artifact Type Version
org.springframework : spring-core jar 5.3.2
org.springframework : spring-webmvc jar 5.3.2
org.springframework.security : spring-security-config jar 5.0.6.RELEASE
org.springframework.data : spring-data-jpa jar 2.1.0.RELEASE
org.hibernate : hibernate-entitymanager jar 5.4.25.Final
javax.servlet : javax.servlet-api jar 4.0.1
org.slf4j : slf4j-api jar 1.7.2
org.slf4j : jcl-over-slf4j jar 1.7.2

test (11)

Group / Artifact Type Version
org.springframework : spring-test jar 5.3.2
org.jmockit : jmockit jar 1.17
junit : junit jar 4.13.1
com.jayway.jsonpath : json-path jar 2.4.0
org.hsqldb : hsqldb jar 2.4.1
javax.validation : validation-api jar 2.0.1.Final
org.hibernate : hibernate-validator jar 6.1.6.Final
javax.el : javax.el-api jar 3.0.0
org.glassfish.web : el-impl jar 2.2
commons-io : commons-io jar 2.6
org.slf4j : slf4j-log4j12 jar 1.7.2

Project Modules

There are no modules declared in this project.

Build Status Codacy Badge BCH compliance codecov Maven Central Javadocs Apache 2

BeanMapper Spring

Input merging argument resolver

Configuration

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    argumentResolvers.add(new MergeFormMethodArgumentResolver(
            Collections.singletonList(mappingJackson2HttpMessageConverter()),
            beanMapper,
            applicationContext,
            entityManager
    ));
}

Usage

@RequestMapping(value = "/bla", method = RequestMethod.POST)
@ResponseBody
public Temp create(@MergeForm(TempForm.class) Temp tempEntity) {
    return tempEntity;
}

@RequestMapping(value = "/bla/{id}", method = RequestMethod.PUT)
@ResponseBody
public Temp update(@MergeForm(value = TempForm.class, id = "id") Temp tempEntity) {
    return tempEntity;
}

MockMvcBeanMapper

Since mapping to an Entity is done at an early stage, your Spring MVC controller level tests must be configured to deal with the usage of repositories. The MockMvcBeanMapper is configured at the level of your abstract controller test, ie the class your controller tests all extend from.

The reason why you need to do this is because:

  • Spring's DomainClassConverter (working on @RequestParam and @PathVariable) makes use of your repositories
  • BeanMapper @MergedForm makes use of your repositories
  • BeanMapper IdToEntityBeanConverter makes use of your repositories

Each of these vectors need to be addressed to set up controller tests that can deal with repositories.

Assuming you use Spring's MockMvcBuilders and assuming you have a web configuration class called WebMvcConfig, this is what you could do:

public abstract class AbstractControllerTest {

    private WebMvcConfig config = new WebMvcConfig();

    protected MockMvc webClient;

    protected MockMvcBeanMapper mockMvcBeanMapper;

    protected void initWebClient(Object controller) {

        this.mockMvcBeanMapper = new MockMvcBeanMapper(
                new FormattingConversionService(),
                Collections.singletonList(config.mappingJackson2HttpMessageConverter()),
                new ApplicationConfig().beanMapper()
        );

        this.webClient = MockMvcBuilders.standaloneSetup(controller)
                .setMessageConverters(config.mappingJackson2HttpMessageConverter())
                .setCustomArgumentResolvers(mockMvcBeanMapper.createHandlerMethodArgumentResolvers())
                .setConversionService(mockMvcBeanMapper.getConversionService())
                .build();
    }

    public BeanMapper beanMapper() {
        return mockMvcBeanMapper.getBeanMapper();
    }

    public void registerRepository(CrudRepository<? extends BaseEntity, Long> repository, Class<?> entityClass) {
        mockMvcBeanMapper.registerRepository(repository, entityClass);
    }

}

In your controller test, you will have to register all the repositories (presumably mock classes) that need to be added, ostensibly in a @Before method..

registerRepository(ownerRepository, Owner.class);

You can take program your mock repositories as you normally would, for example in JMockit:

new NonStrictExpectations() {{
    ownerRepository.findOne(1138L);
    result = new Owner();
}};

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

io.beanmapper

Versions

Version
3.1.0
3.0.0
2.4.1.SPRING5
2.4.1
2.4.0
2.3.2
2.3.0
2.2.0
2.1.0
2.0.2
2.0.1
2.0.0
1.0.0
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1