Created
November 22, 2015 17:52
-
-
Save goeh/b542c7c02462ff4a7a8c to your computer and use it in GitHub Desktop.
Trim strings in JSON paylod
This file contains hidden or 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
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import org.springframework.stereotype.Component; | |
import java.io.IOException; | |
@Component | |
/** | |
* Trim strings in JSON payload. | |
*/ | |
public class StringTrimModule extends SimpleModule { | |
public StringTrimModule() { | |
addDeserializer(String.class, new StdScalarDeserializer<String>(String.class) { | |
@Override | |
public String deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException, | |
JsonProcessingException { | |
final String stringValue = jsonParser.getValueAsString(); | |
switch (jsonParser.getCurrentName()) { | |
case "password": | |
return stringValue; | |
default: | |
return stringValue.trim(); | |
} | |
} | |
}); | |
} | |
} | |
// Application.java: | |
//@Bean | |
//public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { | |
// MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); | |
// ObjectMapper objectMapper = new ObjectMapper(); | |
// objectMapper.registerModule(new StringTrimModule()); | |
// jsonConverter.setObjectMapper(objectMapper); | |
// return jsonConverter; | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment