我收到以下错误。即使我在POJO中有正确的get set方法。
java.lang.IllegalArgumentException:找不到类型返回值的转换器:class com.example.demo.entities.Department 在org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187)〜[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
POJO:
package com.example.demo.entities;
import javax.persistence.*;
@Entity
@Table(name="departments")
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="DEPT_NO")
private Long id;
@Column(name="DEPT_NAME")
private String name;
public Department() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
的Controler
@GetMapping("/departments/{id}")
public ResponseEntity<Department> getDepartmentById(@PathVariable(value = "id") Long departmentId) {
Department department = departmentRepository.findOne(departmentId);
if(department == null) {
return ResponseEntity.notFound().build();
} else {
System.out.println("name is" + department.getName());
}
return ResponseEntity.ok().body(department);
}
不确定,这是因为我有正确的设置方法。
请帮忙。
答案 0 :(得分:0)
您需要将Jackson依赖项添加到您的pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
如果您未对控制器使用@RestController
注释,则需要在@ResponseBody
方法中添加@GetMapping
注释。
@ResponseBody
@GetMapping("/departments/{id}")