找不到类型返回值的转换器

时间:2017-05-07 01:13:28

标签: json spring rest spring-mvc jackson

  

模型类(为了简洁而省略了getter和setter)

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;


@Entity
@Table(name = "users")
public class User implements Serializable {

@Id
@GeneratedValue
@Column(name = "id")
private Long id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "birthday")
private LocalDate birthday;

@Column(name = "login")
private String login;

@Column(name = "password")
private String password;

@Column(name = "info")
private String info;

@ManyToOne(targetEntity = UserAddress.class, fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
@JoinColumn(name = "address_id")
private UserAddress homeAddress;


public User() {
}

public User(String firstName, String lastName, LocalDate birthday, String login, String password, String info, UserAddress homeAddress) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthday = birthday;
    this.login = login;
    this.password = password;
    this.info = info;
    this.homeAddress = homeAddress;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public LocalDate getBirthday() {
    return birthday;
}

public void setBirthday(LocalDate birthday) {
    this.birthday = birthday;
}

public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getInfo() {
    return info;
}

public void setInfo(String info) {
    this.info = info;
}

public UserAddress getHomeAddress() {
    return homeAddress;
}

public void setHomeAddress(UserAddress homeAddress) {
    this.homeAddress = homeAddress;
}

@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", firstName='" + firstName + '\'' +
            ", lastName='" + lastName + '\'' +
            ", birthday=" + birthday +
            ", login='" + login + '\'' +
            ", password='" + password + '\'' +
            ", info='" + info + '\'' +
            ", homeAddress=" + homeAddress +
            '}';
    }
}
  

控制器

@RequestMapping(value = "/rest")
@RestController
public class RestUserController {
    @GetMapping(value = {"/test"})
    public ResponseEntity<User> getUser() {
        User user = new User();
        return new ResponseEntity<>(user, HttpStatus.OK);
    }
}
  

配置

@Configuration
public class SpringWebConfig extends WebMvcConfigurerAdapter {
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new MappingJackson2HttpMessageConverter());
    super.configureMessageConverters(converters);
  }
} 
  

Maven POM

    <!--JACKSON-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.7</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.7</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.7</version>
    </dependency>
    //....

    <!--SPRING-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    //....

但是,调用REST端点会引发错误No converter found for return value of type...

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class com.models.users.User
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

请帮助,我在哪里可以找到更多信息来解决此错误?

0 个答案:

没有答案