sansari
sansari

Reputation: 558

No Suitable MessageConverter

I have a spring boot application and am testing integration test. My REST service produces JSON and I can confirm it when testing it in postman.

But when I make a getForObject call by restTemplate:

 @Test
    public void testGetObject() {
        Pet pet = restTemplate.getForObject("http://localhost:9000/pets/10000", User.class, Collections.emptyMap());
        assertThat(pet.getName(), is("Bobby"));
    }

It fails with following error:

Could not extract response: no suitable HttpMessageConverter found for response type [class petstore.entity.User] and content type [text/html;charset=utf-8]

I read lots of posts in stackoverflow, and having that restTempalte itself has MappingJackson2HttpMessageConverter as one of default converters which has JSON as default media type then I should not get this error.

Is there anything I am missing here?

Upvotes: 0

Views: 503

Answers (1)

Miloš Milivojević
Miloš Milivojević

Reputation: 5369

Well, the message is pretty indicative - you're getting text/html as a response type, make your endpoint return application/json. If you're using Spring MVC then you can do it by adding the produces parameter to the annotation:

@RequestMapping(value = "/pets/{id}", method = RequestMethod.GET, produces = "application/json")

Or, if you're using Jersey, add this annotation to your method:

@Produces("application/json")

Upvotes: 1

Related Questions