SpringMvc No converter fo * with persent Content-Type 'null'

186 views Asked by At

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
0

There are 0 answers