java.lang.IllegalArgumentException: No converter found for return value of type

后端 未结 20 1870
夕颜
夕颜 2020-11-30 00:04

With this code

@RequestMapping(value = \"/bar/foo\", method = RequestMethod.GET)
    public ResponseEntity foo() {

        Foo model;
        ...         


        
相关标签:
20条回答
  • 2020-11-30 00:50

    I was facing same issue for long time then comes to know have to convert object into JSON using Object Mapper and pass it as JSON Object

    @RequestMapping(value = "/getTags", method = RequestMethod.GET)
    public @ResponseBody String getTags(@RequestParam String tagName) throws
            JsonGenerationException, JsonMappingException, IOException {
        List<Tag> result = new ArrayList<Tag>();
        for (Tag tag : data) {
            if (tag.getTagName().contains(tagName)) {
                result.add(tag);
            }
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);
        return json;
    }
    
    0 讨论(0)
  • 2020-11-30 00:50

    In my case i'm using spring boot , and i have encountered a similar error :

    No converter for [class java.util.ArrayList] with preset Content-Type 'null'
    
    

    turns out that i have a controller with

    @GetMapping(produces = { "application/xml", "application/json" })
    
    

    and shamefully i wasn't adding the Accept header to my requests

    0 讨论(0)
  • 2020-11-30 00:52

    Add the getter/setter missing inside the bean mentioned in the error message.

    0 讨论(0)
  • 2020-11-30 00:53

    In my case, I forgot to add library jackson-core.jar, I only added jackson-annotations.jar and jackson-databind.jar. When I added jackson-core.jar, it fixed the problem.

    0 讨论(0)
  • 2020-11-30 00:53

    In my case, I was returning Boolean in Response Entity and had :

    produces = MediaType.TEXT_PLAIN_VALUE,
    

    When i changed it to below

    produces = MediaType.APPLICATION_JSON_VALUE
    

    It worked!

    Example of what i had.

    @PostMapping(value = "/xxx-xxxx",
                produces = MediaType.APPLICATION_JSON_VALUE,
                consumes = MediaType.APPLICATION_JSON_VALUE)
        public ResponseEntity<Boolean> yyyy(
    
    0 讨论(0)
  • 2020-11-30 00:56

    While using Spring Boot 2.2 I run into a similiar error message and while googling my error message

    No converter for [class java.util.ArrayList] with preset Content-Type 'null'

    this question here is on top, but all answers here did not work for me, so I think it's a good idea to add the answer I found myself:

    I had to add the following dependencies to the pom.xml:

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
    </dependency>
    
    <dependency>
      <groupId>com.thoughtworks.xstream</groupId>
      <artifactId>xstream</artifactId>
      <version>1.4.11.1</version>
    </dependency>
    

    After this I need to add the following to the WebApplication class:

    @SpringBootApplication
    public class WebApplication
     {
      // ...
    
      @Bean
      public HttpMessageConverter<Object> createXmlHttpMessageConverter()
       {
        final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
        final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
        xstreamMarshaller.setAutodetectAnnotations(true);
        xmlConverter.setMarshaller(xstreamMarshaller);
        xmlConverter.setUnmarshaller(xstreamMarshaller);
        return xmlConverter;
       }
    
    }
    

    Last but not least within my @Controller I used:

    @GetMapping(produces = {MediaType.APPLICATION_XML_VALUE, MediaType. APPLICATION_JSON_VALUE})
    @ResponseBody
    public List<MeterTypeEntity> listXmlJson(final Model model)
     {
      return this.service.list();
     }
    

    So now I got JSON and XML return values depending on the requests Accept header.

    To make the XML output more readable (remove the complete package name from tag names) you could also add @XStreamAlias the following to your entity class:

    @Table("ExampleTypes")
    @XStreamAlias("ExampleType")
    public class ExampleTypeEntity
     {
      // ...
     }
    

    Hopefully this will help others with the same problem.

    0 讨论(0)
提交回复
热议问题