3

I am trying to expose REST service, but while hitting it from POSTMAN i am getting below :

WARNING: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.ArrayList

Where as i have also included below jar files which are required :

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.1</version>
    </dependency>

Here is my REST controller Code :

@RestController
@RequestMapping("/MayankAPI")
public class TestRestAPI {
@RequestMapping(value="/sayHello" , method = RequestMethod.POST)
public TestPojo postData(@RequestBody String payload) {
  System.out.println("Hello post"+payload);
  TestPojo payload1=new TestPojo();
  payload1.setStudentName("Jack");
  return payload1;
}
@RequestMapping(value="/sayHello" , method = RequestMethod.GET)
public List<TestPojo> getData(String payload) {
  System.out.println("Hello get"+payload);
  List<TestPojo> payload1=new ArrayList<TestPojo>();
  TestPojo tp = new TestPojo();
  tp.setStudentName("Jack");
  payload1.add(tp);
  return payload1;
}

Here is my bean which i am trying to return :

public class TestPojo {

  private String studentName;
  private String studentId;

  public String getStudentName() {
    return studentName;
  }

  public void setStudentName(String studentName) {
    this.studentName = studentName;
  }

  public String getStudentId() {
    return studentId;
  }

  public void setStudentId(String studentId) {
    this.studentId = studentId;
  }
}

Please help me where i am doing wrong.

  • Right there, in your error message, "ArrayList cannot be converted...". Try using the .ToArray() function on the ArrayList before returning it. – Callback Kid Nov 15 '17 at 18:56
  • but why would i convert it to array since i want to return list. even in that case it doesn't work. – Mayank Aggarwal Nov 15 '17 at 19:01
  • I am not familiar with JSON conversion in Java, but from the error, it seems like it is not able to convert your ArrayList to a JSON string. – Callback Kid Nov 15 '17 at 19:04
  • Also look [here](https://stackoverflow.com/questions/33832735/spring-boot-application-no-converter-found-for-return-value-of-type). Can you try making your objects properties public? It may be a problem with the getters. – Callback Kid Nov 15 '17 at 19:09
  • nope still getting same error :( – Mayank Aggarwal Nov 15 '17 at 19:15
  • 1
    Something probably fails with content type negotiation. Make sure you have [MappingJackson2HttpMessageConverter](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html) registered – Mikk Nov 15 '17 at 20:19
  • Thanks. Yes i also figured out same last night and its working now. – Mayank Aggarwal Nov 16 '17 at 05:57

2 Answers2

4

I know it too late but still Alternate Solution: you need to enable Spring project as Web MVC as follow:

@Configuration @EnableWebMvc @ComponentScan("basePackages = com.test") public class MiBenefitConfiguration { }

amoljdv06
  • 2,646
  • 1
  • 13
  • 18
3

This is happening because MappingJackson2HttpMessageConverter is not registered in my App config file as below.

@Configuration
@ComponentScan("basePackages = com.test")
public class MiBenefitConfiguration extends WebMvcConfigurationSupport{

  @Bean
  public ObjectMapper getObjectMapper() {
    return new ObjectMapper();
  }

  @Bean
  public MappingJackson2HttpMessageConverter messageConverter() {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(getObjectMapper());
    return converter;
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(messageConverter());
    addDefaultHttpMessageConverters(converters);
  }
}