I'm using org.springframework:spring-web:5.1.9.RELEASE and have a plain (not customized) RestTemplate class object I use to send a POST request by utilizing the EXCHANGE method. I want to allow it desirialize unknown ENUM values from the response to their default value (as set using #JsonEnumDefaultValue) instead of failing the whole operation. I've searched and didn't find any easy way to do it, can you give me some help with that? Thanks!
#Service
#CommonsLog
public class TMServerExternalApiRepositoryImpl implements TMServerExternalApiRepository {
private final RestTemplate restRelay;
private TeleMessageProperties tmUrls;
#Autowired
public TMServerExternalApiRepositoryImpl(RestTemplate restTemplate, TeleMessageProperties tmProperties) {
this.restRelay = restTemplate;
this.tmUrls = tmProperties;
}
#Override
public VnvUsersSearchResult getUsersByPhone(String to, String from) {
log.info(String.format("Trying to get users with telephones to: [%s], from: [%s] ", to, from));
return sendRequest(new VnvUserSearchParams(from, to), VnvUsersSearchResult.class, tmUrls.getGetUserByPhoneUrl()).getBody();
}
//the actual post
private <T, K> ResponseEntity<T> sendRequest(K content, Class<T> returnTypeClass, String url) throws HttpClientErrorException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<K> httpEntity = new HttpEntity<>(content, httpHeaders);
return restRelay.exchange(url, HttpMethod.POST, httpEntity, returnTypeClass);
}
}
This is the exception I am getting now:
org.springframework.web.client.RestClientException: Error while extracting response for type [class voiceAndVideo.services.VnvUsersSearchResult] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `voiceAndVideo.Util.VNVDevice$Type` from String "BAD_VALUE": value not one of declared Enum instance names: [MOBILE, ...]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `voiceAndVideo.Util.VNVDevice$Type` from String "BAD_VALUE": value not one of declared Enum instance names: [MOBILE, ...]
at [Source: (ByteArrayInputStream); line: 1, column: 261] (through reference chain: voiceAndVideo.services.VnvUsersSearchResult["userFrom"]->voiceAndVideo.Util.VNVUser["devices"]->java.util.ArrayList[2]->voiceAndVideo.Util.VNVDevice["type"])
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:117) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
Classes:
public class VnvUsersSearchResult {
private VNVUser userFrom;
...
}
public class VNVUser {
...
protected List<VNVDevice> devices;
...
}
public class VNVDevice {
public Type type;
String mobile;
public enum Type {
#JsonEnumDefaultValue
UNKNOWN(0),
MOBILE(10),
BUSINESS_PHONE(20),
...
int ID;
Type(int i) {
this.ID = i;
}
}
}
You probably need to configure a custom ObjectMapper to the RestTemplate that you are using. You need to enable this feature on the ObjectMapper. Please make sure you are using fasterxml as the package for all these.
In order to configure one, create a JavaConfig file like this:
#Bean
public RestOperations restOperations() {
RestTemplate rest = new RestTemplate();
rest.getMessageConverters().add(0, mappingJacksonHttpMessageConverter());
return rest;
}
#Bean
public MappingJacksonHttpMessageConverter mappingJacksonHttpMessageConverter() {
MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
converter.setObjectMapper(myObjectMapper());
return converter;
}
#Bean
public ObjectMapper myObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
// This where you enable default enum feature
objectMapper.configure(DeserializationFeature. READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
return objectMapper;
}
I was able to solve it using JsonCreator placed in the ENUM class.
#JsonCreator
public static Type getByValue(String t) {
return Arrays.stream(Type.values())
.filter(a -> a.name().equals(t)).findFirst().orElse(Type.UNKNOWN);
}
}
Related
I'm trying to parse the JSON from Github's commit details API into a HashMap. The Sample Json I've used in my tests is here
The test code is
#SpringJUnitConfig(classes = {GithubApiService.class, RestTemplateConfiguration.class})
#EnableAutoConfiguration
public class GithubApiServiceTest {
#Test
public void testGithubResponseJsonToMapConversion(
#Autowired RestTemplate restTemplate,
#Autowired GithubApiService service,
#Value("classpath:github/commit-payload.json") Resource commitPayloadFile) throws IOException {
final String COMMITS_URL = "https://api.github.com/repos/Codertocat/Hello-World/commits/sha";
//Stub response from Github Server
String responseJson = new ObjectMapper().writeValueAsString(
new String(Files.readAllBytes(Paths.get(commitPayloadFile.getFile().getAbsolutePath()))));
MockRestServiceServer mockGithubServer = MockRestServiceServer.createServer(restTemplate);
mockGithubServer.expect(requestTo(COMMITS_URL))
.andRespond(withSuccess().contentType(APPLICATION_JSON).body(responseJson));
//Call API Service
Map<String, Object> result = service.getCommitDetails(COMMITS_URL);
//Expect return type is hashmap
assertThat(result.get("sha")).isEqualTo("6dcb09b5b57875f334f61aebed695e2e4193db5e");
}
}
The Service code is
#Service
#AllArgsConstructor(onConstructor = #__(#Autowired))
public class GithubApiService {
#Autowired
private final RestTemplate restTemplate;
public Map<String, Object> getCommitDetails(String commitsUrl) {
ParameterizedTypeReference<Map<String, Object>> responseType = new ParameterizedTypeReference<Map<String, Object>>() {
};
RequestEntity<Void> request = RequestEntity.get(URI.create(commitsUrl)).accept(APPLICATION_JSON).build();
return restTemplate.exchange(request, responseType).getBody();
}
}
This fails converting the JSON response into a Map with the following error (full log here)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==",
"html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments",
Since I'm using spring-boot-starter-web it auto-wires converters including MappingJackson2HttpMessageConverter but debugger shows that the response is being processed by the ByteArrayHttpMessageConverter despite setting content type to application/json.
My bad, I should have used the #RestClientTest annotation intead of #EnableAutoConfiguration, that fixed the problem.
#EnableAutoConfiguration is a more generic annotation which doesn't achieve all the auto config that #RestClientTest does
I'm having a trouble with deserializing JSON like:
[{
"foo": "bar",
"example": "value"
}]
For some reason, API I use packs it in unnamed Array with one element. I'm accessing that API with RestController.
As long as I used default Jackson Converter, I could easily prepare model classes like that (getters, setters, constructors omitted for clarity):
public class Model {
private String foo;
private String example;
}
public class Response {
#JsonValue
private List<Model> model;
}
Repository class
public class FooRepository {
private RestTemplate restTemplate;
public String getFoo() {
ResponseEntity<Response> response =
restTemplate
.exchange("http://localhost:8080/api"
HttpMethod.GET,
new HttpEntity<>(new HttpHeaders()),
Response.class);
return response.getBody().getModel().get(0).getFoo();
}
I cannot find any annotation that works like #JsonValue for Jsonb. Is there one? Or maybe there is another way to fix my problem without using Jackson?
edit:
I figured out a workaround solution.
public String getFoo() {
ResponseEntity<List<Model>> response =
restTemplate
.exchange("http://localhost:8080/api"
HttpMethod.GET,
new HttpEntity<>(new HttpHeaders()),
ParameterizedTypeReference<List<Model>>());
return response.getBody().get(0).getFoo();
}
Given the following basic domain model:
abstract class BaseData { ... }
class DataA extends BaseData { ... }
class DataB extends BaseData { ... }
I want to write a Spring MVC controller endpoint thus ...
#PostMapping(path="/{typeOfData}", ...)
ResponseEntity<Void> postData(#RequestBody BaseData baseData) { ... }
The required concrete type of baseData can be inferred from the typeOfData in the path.
This allows me to have a single method that can handle multiple URLs with different body payloads. I would have a concrete type for each payload but I don't want to have to create multiple controller methods that all do the same thing (albeit each would do very little).
The challenge that I am facing is how to "inform" the deserialization process so that the correct concrete type is instantiated.
I can think of two ways to do this.
First use a custom HttpMessageConverter ...
#Bean
HttpMessageConverter httpMessageConverter() {
return new MappingJackson2HttpMessageConverter() {
#Override
public Object read(final Type type, final Class<?> contextClass, final HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
// TODO How can I set this dynamically ?
final Type subType = DataA.class;
return super.read(subType, contextClass, inputMessage);
}
};
}
... which gives me the challenge to determine the subType based on the HttpInputMessage. Possibly I could use a Filter to set a custom header earlier when the URL is available to me, or I could use a ThreadLocal also set via a Filter. Neither sounds ideal to me.
My second approach would be to again use a Filter and this time wrap the incoming payload in an outer object which would then provide the type in a way that enables Jackson to do the work via #JsonTypeInfo. At the moment this is probably my preferred approach.
I have investigated HandlerMethodArgumentResolver but if I try to register a custom one it is registered AFTER the RequestResponseBodyMethodProcessor and that class takes priority.
Hmm, so after typing all of that out I had a quick check of something in the RequestResponseBodyMethodProcessor before posting the question and found another avenue to explore, which worked neatly.
Excuse the #Configuration / #RestController / WebMvcConfigurer mash-up and public fields, all for brevity. Here's what worked for me and achieved exactly what I wanted:
#Configuration
#RestController
#RequestMapping("/dummy")
public class DummyController implements WebMvcConfigurer {
#Target(ElementType.PARAMETER)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#interface BaseData {}
public static class AbstractBaseData {}
public static class DataA extends AbstractBaseData {
public String a;
}
public static class DataB extends AbstractBaseData {
public String b;
}
private final MappingJackson2HttpMessageConverter converter;
DummyController(final MappingJackson2HttpMessageConverter converter) {
this.converter = converter;
}
#Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(
new RequestResponseBodyMethodProcessor(Collections.singletonList(converter)) {
#Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(BaseData.class)
&& parameter.getParameterType() == AbstractBaseData.class;
}
#Override
protected <T> Object readWithMessageConverters(
NativeWebRequest webRequest, MethodParameter parameter, Type paramType)
throws IOException, HttpMediaTypeNotSupportedException,
HttpMessageNotReadableException {
final String uri =
webRequest.getNativeRequest(HttpServletRequest.class).getRequestURI();
return super.readWithMessageConverters(
webRequest, parameter, determineActualType(webRequest, uri));
}
private Type determineActualType(NativeWebRequest webRequest, String uri) {
if (uri.endsWith("data-a")) {
return DataA.class;
} else if (uri.endsWith("data-b")) {
return DataB.class;
}
throw new HttpMessageNotReadableException(
"Unable to determine actual type for request URI",
new ServletServerHttpRequest(
webRequest.getNativeRequest(HttpServletRequest.class)));
}
});
}
#PostMapping(
path = "/{type}",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<? extends AbstractBaseData> post(#BaseData AbstractBaseData baseData) {
return ResponseEntity.ok(baseData);
}
}
The key to this is that I stopped using #RequestBody because that is what was preventing me overriding the built-in behaviour. By using #BaseData instead I get a HandlerMethodArgumentResolver that uniquely supports the parameter.
Other than that it was a case of assembling the two objects that already did what I needed, so autowire a MappingJackson2HttpMessageConverter and instantiate a RequestResponseBodyMethodProcessor with that one converter. Then pick the right method to override so that I could control what parameter type was used at a point that I had access to the URI.
Quick test. Given the following payload for both requests ...
{
"a": "A",
"b": "B"
}
POST http://localhost:8081/dummy/data-a
... gives a response of ...
{
"a": "A"
}
POST http://localhost:8081/dummy/data-b
... gives a response of ...
{
"b": "B"
}
In our real-world example this means that we will be able to write one method each that supports the POST / PUT. We need to build the objects and configure the validation possibly - or alternatively if we use OpenAPI 3.0 which we are investigating we could generate the model and validate without writing any further code ... but that's a separate task ;)
I am reading a JSON response back from Mailchimp's Mandrill API with jackson. The response is a bit unconventional for an API response in that it includes handlebars inside of square brackets - a list of objects. The other stack overflow discussions around this error pertain to API responses that are not in a list.
[
{
"email": "gideongrossman#gmail.com",
"status": "sent",
"_id": "6c6afbd3702f4fdea8de690c284f5898",
"reject_reason": null
}
]
I am getting this error...
2019-07-06 22:41:47.916 DESKTOP-2AB6RK0 core.RestClient 131222 ERROR com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `core.user.MandrillWrapper$TemplatedEmailResponse` out of START_ARRAY token
What is the correct way to define this response object?
I have tried defining the response with the following types. None worked.
public static class TemplatedEmailResponse {
public LinkedHashMap<String, String>[] response;
}
public static class TemplatedEmailResponse {
public ArrayList<LinkedHashMap<String, String>> response;
}
#milchalk...How exactly can I use your objectmapper suggestion with the way I am currently calling the API and handling the response?
TemplatedEmailResponseList ret = getClient("messages/send-template.json").post(mandrillPayload,
TemplatedEmailResponseList.class);
where
public <T> T post(Object payload, Class<T> responseType) {
try {
Entity<Object> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
T t = client.target(url).request(MediaType.APPLICATION_JSON).post(entity, responseType);
return t;
} catch (Throwable t) {
logError(t);
throw t;
} finally {
client.close();
}
}
You can deserialize this json directly to List of your Pojo classes.
Given model class :
public class TemplatedEmailResponse {
private String email;
private String status;
private String _id;
private String reject_reason;
//getters setters
}
You can deserialize this json using TypeReference for List<TemplatedEmailResponse> :
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<TemplatedEmailResponse>> typeRef = new TypeReference<List<TemplatedEmailResponse>>() {};
List<TemplatedEmailResponse> list = mapper.readValue(json, typeRef);
Where json variable represents the json string in this case.
In my application I configured Jackson to use SerializationFeature.WRAP_ROOT_VALUE and DeserializationFeature.UNWRAP_ROOT_VALUE globally.
#Configuration
public class AppConfig {
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToEnable(SerializationFeature.WRAP_ROOT_VALUE, DeserializationFeature.UNWRAP_ROOT_VALUE);
return builder;
}
}
This configuration works fine but now I am in a situation where in deserialization case I get a JSON Response without rootname. So I have got a Service Class which builds a RestTemplate using RestTemplateBuilder and POST some Data to a REST-Webservice.
#Service
public class ApiServiceImpl
implements ApiService<RegisterResponse> {
private RestTemplate restTemplate;
public ApiServiceImpl(RestTemplateBuilder restTemplateBuilder) {
restTemplate = restTemplateBuilder
.errorHandler(new RestTemplateResponseErrorHandler()).build();
}
#Override
public ResponseEntity<RegisterResponse> callAPI(String requestAsJson,
String username, String password) {
ResponseEntity<RegisterResponse> result = null;
HttpHeaders headers = getHeaders(username, password);
result = restTemplate.exchange(uri, HttpMethod.POST,
new HttpEntity<String>(requestAsJson, headers),
RegisterResponse.class);
return result;
}
}
The Response looks like the following:
{
"redirect-url": "https://any-url.com/?with=params"
}
And I want to deserialize this to the following POJO directly. (Like in restTemplate.exchange configured)
public class RegisterResponse {
#JsonProperty("redirect-url")
private String redirectUrl;
//getter/setter
}
It's clear to get this exception because of the UNWRAP_ROOT_VALUE Feature:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'redirect-url' does not match expected ('RegisterResponse') for type [simple type, class xxx.xxx.xxxservice.xxx.model.response.entity.RegisterResponse]
at [Source: (String)"{
"redirect-url": "https://any-url.com/?with=params"
}"; line: 2, column: 5]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1356)
at com.fasterxml.jackson.databind.ObjectMapper._unwrapAndDeserialize(ObjectMapper.java:4087)
How can I configure Jackson to dont use DeserializationFeature.UNWRAP_ROOT_VALUE in this particular case?
Like JB Nizet commented, its possibly by setting a new Instance of MappingJackson2HttpMessageConverter and ObjectMapper for Jakson to the list of MessageConverters.
restTemplate.getMessageConverters().add(getCustomConverter());
private MappingJackson2HttpMessageConverter getCustomConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
MappingJackson2HttpMessageConverter customConverter =
new MappingJackson2HttpMessageConverter(mapper);
if (!restTemplate.getMessageConverters()
.removeIf(MappingJackson2HttpMessageConverter.class::isInstance)) {
new RuntimeException("Custom MappingJackson2HttpMessageConverter not found");
}
return customConverter;
}