Reputation: 8798
When I try to use POJO with MediaType.APPLICATION_FORM_URLENCODED
ExampleRequest exampleRequest = exampleRequest();
exampleRequest.setId("id");
exampleRequest.setName("name");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<ExampleRequest> exampleRequestEntity = new HttpEntity<>(exampleRequest, headers);
restTemplate.postForObject(url, exampleRequestEntity, String.class)
I get:
org.springframework.web.client.RestClientException: No HttpMessageConverter for [ExampleRequest] and content type [application/x-www-form-urlencoded]
Is there any way to use POJO with application/x-www-form-urlencoded
instead of MultiValueMap<String, String> map
?
Upvotes: 5
Views: 4125
Reputation: 1467
FormHttpMessageConverter
might be missing. Have you tried adding it manually?
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FormHttpMessageConverter converter = new FormHttpMessageConverter();
MediaType mediaType = new MediaType("application","x-www-form-urlencoded", Charset.forName("UTF-8"));
converter.setSupportedMediaTypes(Arrays.asList(mediaType));
converters.add(converter);
super.configureMessageConverters(converters);
}
}
(Relevant for spring boot)
Upvotes: 2
Reputation: 44398
There is possible to use the FormHttpMessageConverter
which is able to convert data to/from a MultiValueMap<String, String>
required for the application/x-www-form-urlencoded
media type. Add the following configuration to the class implementing WebMvcConfigurer.
@Bean
public FormHttpMessageConverter formHttpMessageConverter() {
MediaType mediaType = new MediaType("application", "x-www-form-urlencoded",
Charset.forName("UTF-8"))
FormHttpMessageConverter formHttpMessageConverter= new FormHttpMessageConverter();
formHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(mediaType));
return formHttpMessageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(formHttpMessageConverter());
super.configureMessageConverters(converters);
}
Upvotes: 0