Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Auto Trim Strings of bean object in spring with Restful api?

Tags:

java

rest

spring

I want to trim all the form string fields trim automatically (trailing & leading spaces only)

Suppose if I pass FirstName = " robert " Expected: "robert"

Controller class having following code :

@InitBinder
public void initBinder ( WebDataBinder binder )
{
    StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);  
    binder.registerCustomEditor(String.class, stringtrimmer);
}

@RequestMapping(value = "/createuser", method = RequestMethod.POST)
public Boolean createUser(@RequestBody  UserAddUpdateParam userAddUpdateParam) throws Exception {

    return userFacade.createUser(userAddUpdateParam);
}  

when I debug the code, It's coming into @InitBinder but not trimming bean class string fields.

like image 566
bhanwar rathore Avatar asked Feb 21 '17 08:02

bhanwar rathore


2 Answers

The annotation @InitBinder doesn't work with @RequestBody, you have to use it with the @ModelAttribute annotation

You can find more information in the Spring documentation:

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

like image 125
mlg Avatar answered Sep 17 '22 19:09

mlg


To add this feature to all JSON submitted in post and received in RequestBody you can have following WebMvcConfigurer in place.

    import java.util.List;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.module.SimpleModule;

    @Configuration
    public class HttpMessageConvertor implements WebMvcConfigurer {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        SimpleModule module = new SimpleModule();
        module.addDeserializer(String.class, new StringWithoutSpaceDeserializer(String.class));
        mapper.registerModule(module);

        converter.setObjectMapper(mapper);

        return converter;
    }
}

StringWithoutSpaceDeserializer class as :

import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class StringWithoutSpaceDeserializer extends StdDeserializer<String> {
    /**
     * 
     */
    private static final long serialVersionUID = -6972065572263950443L;

    protected StringWithoutSpaceDeserializer(Class<String> vc) {
        super(vc);
    }

    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return p.getText() != null ? p.getText().trim() : null;
    }
}
like image 44
Ashu Avatar answered Sep 16 '22 19:09

Ashu