一个Spring Boot控制器的报错

youyiyang 2018-10-09 03:08:17
本人在用Spring Boot开发一个项目的时候,碰到一个通过控制器不能正确显示页面的问题:
控制器的代码如下:

/**
* User控制器
* @author EDDIEYOU
*
*/

@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
/**
* 查询所有用户
* @param model
* @return
*/
@GetMapping
public ModelAndView list(Model model) {
System.out.println("进入Users控制器");
model.addAttribute("userList",userRepository.listUsers());
model.addAttribute("title","用户管理");
return new ModelAndView("users/list","userModel",model);
}
}

这个控制器就是让用户在浏览器上输入http://localhost:8080/users后能在页面上显示list.html
list.html是用的thymeleaf模板:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title>List</title>
</head>
<body>
<div th:replace="~{fragments/header :: header}"></div>
<h3 th:text="${userModel.title}">列表</h3>
<div>
<a href="/users/form.html" th:href="@{/users/form}">创建用户</a>
</div>
<table border="1">
<thead>
<tr>
<td>ID</td>
<td>Email</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<tr th:if="${userModel.userlist.size()} eq 0">
<td colspan="3">没有用户信息!</td>
</tr>
<tr th:each="user : ${userModel.userlist}">
<td th:text="${user.id}"></td>
<td th:text="${user.email}"></td>
<td><a th:href="@{'/users/'+${user.id}}" th:text="${user.name}"></a></td>
</tr>
</tbody>
</table>

<div th:replace="~{fragments/footer :: footer}"></div>
</body>
</html>

但是现在页面报这个错:
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "list"
后台Console上显示:

进入Users控制器
2018-10-09 15:04:01.068 WARN 7568 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "list"

User实体是这么定义的:

package com.eddie.spring.boot.domain;

/**
* @author EDDIEYOU
* 用户实体
*/
public class User {
private Long id; //实体唯一标示
private String name;
private String email;

public User() { //无参构造函数

}

public User(Long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}

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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}


请问“Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'”是哪里写的不对?谢谢!
...全文
504 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
youyiyang 2018-10-10
  • 打赏
  • 举报
回复
这个问题解决了。是因为thymeleaf没有配置好,并且配置好后需要build或者refresh dependencies一下
zzzerd 2018-10-09
  • 打赏
  • 举报
回复
看下数据库user表中id是不是有字符串,实体类中是long类型,如果有字符串是没办法转long类型的

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧