访问链接http://localhost:8080/test/user,若报错java.lang.IllegalArgumentException: No converter found for return value of type,则是UserEntity类中缺少getter/setter方法
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.0</version>
</dependency>
package com.hundsun.test.entity;
public class UserEntity {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserEntity(){
}
public UserEntity(String username, String password){
this.username = username;
this.password = password;
}
}
package com.hundsun.test.controller;
import com.hundsun.test.entity.UserEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")//Contoller下所有接口统一入口
public class UserController {
//映射一个action
@RequestMapping("/user")
@ResponseBody//表示直接输出返回内容,不进行jsp或html跳转,本例是为了写接口,这里直接返回json
public UserEntity getUser() {
//创建一个UserEntity,直接返回,之前在web.xml中配置的jackson会将user对象转为json输出
UserEntity user = new UserEntity("jack", "123456");
return user;
}
}