So far the best alternativity for me was write a converter.
My points to choose this way are:
- I need to deal with only one endpoint (and only one object) in my API which are different for others endpoints that I have control over them;
- I have not control about the thirdy party service that call my endpoint;
- The thirdy party service call in only one way my services, via POST with media type as
application/x-www-form-urlencoded
.
First: write an object
package xx.xx.xx;
import java.io.Serializable;
public class MyObject implements Serializable {
private static final long serialVersionUID = -7474838713815803531L;
private String name;
private String id;
...
getters
setters
...
}
Second: create a converter to mapping the model
package xx.xx.xx;
import java.io.IOException;
import java.util.Map;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.fasterxml.jackson.databind.ObjectMapper;
import xx.xx.xx.MyObject;
public class MyObjectConverter extends AbstractHttpMessageConverter<MyObject> {
private static final FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
private static final ObjectMapper mapper = new ObjectMapper();
@Override
protected boolean supports(Class<?> clazz) {
return (MyObject.class == clazz);
}
@Override
protected MyObject readInternal(Class<? extends MyObject> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
Map<String, String> vals = formHttpMessageConverter.read(null, inputMessage).toSingleValueMap();
return mapper.convertValue(vals, MyObject.class);
}
@Override
protected void writeInternal(MyObject myObject, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
}
}
Third: tell to spring use this converter
package xx.xx.xx;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class HppResponseConverterDTOConfig implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
}
private MyObjectConverter converter() {
MyObjectConverter converter = new MyObjectConverter();
MediaType mediaType = new MediaType("application", "x-www-form-urlencoded", Charset.forName("UTF-8"));
converter.setSupportedMediaTypes(Arrays.asList(mediaType));
return converter;
}
}
Bonus: this configuration/implementation deal with underscore character inside attributes, in other words: you can use @JsonProperty
annotation because this implementation above use ObjectMapper
and object Mapper (I think) is the same that spring use to serialize normal POST's with Content-type: application/json
.
Fourth and final: using this implementation:
package xx.xx.xx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/my-object-controller")
public class MyObjectController {
@PostMapping(value = "/something", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void doSomething(@RequestBody MyObject myObject) {
doSomethingWithMyObject(myObject);
}
}
I hope it helps :)
EDIT: I'm using spring boot 2