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.
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
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With