0

I tried to write an SSM project, but when I used the Controller to return my custom object, it gave me a No converter for [class cn.pickle.entiry.Student] with preset Content-Type 'null']

error message

19-Dec-2022 17:43:13.011 警告 [http-nio-8080-exec-6] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class cn.pickle.entiry.Student] with preset Content-Type 'null']

returned object

@Getter
@Setter
public class Student {
    private int age;
    private String name;
}

mycontroller


@RestController
@RequestMapping(value = "/demo")
public class demo {
    @GetMapping(value = "/demo2",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Student demo2(){
        final Student student = new Student();
        student.setAge(11);
        student.setName("pickle");
        return student;
    }
}

partial pom.xml

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.20</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.20</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.20</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.20</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.14.0</version>
    </dependency>
</dependencies>

I use

<mvc:annotation-driven/>

in springmvc-config.xml, but it doesn't work;

How can I return the Object directly from the controller
pickle
  • 1
  • You are missing `content-type` header while sending request, also for your consistency, add `@consumes` annotation as well in the controller – Harsh Dec 19 '22 at 10:02
  • doesn's work,it return the same error message. – pickle Dec 19 '22 at 12:45
  • I added awarto the pom file, which successfully solved the problem – pickle Dec 19 '22 at 14:43
  • since that wasn't mentioned in the POM you posted, could not see it but a similar issue has been due to what I also mentioned – Harsh Dec 20 '22 at 04:43

0 Answers0