已解决 [org.springframework.beans.TypeMismatchException:问题
我正在尝试使用 Spring Boot 构建一个 Twitter 克隆网站。我收到错误并查看了我的代码。我不确定我做错了什么:
2022-03-15 12:23:51.067 WARN 4580 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.example.demo.Tweet'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'Test'; nested exception is java.lang.NumberFormatException: For input string: "Test"]
这是一个使用 postgres 作为数据库的 springboot 应用程序。我的代码中还有一个用户存储库,可以正确登录我。如果有任何帮助,我将不胜感激。谢谢。
编辑:添加了完整的堆栈跟踪。 Edit2:添加了 tweeting.html 的图像以显示字符串“Test”的来源。
Tweet.java
package com.example.demo;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
@Entity
@Table(name="tweets")
public class Tweet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id",unique=true, nullable = false)
private Integer id;
@Column(name="userid", unique=true, nullable=false)
private Integer userid;
@Column(name="tweet", nullable=false)
private String tweet;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userid;
}
public void setUserId(Integer userid) {
this.userid = userid;
}
public String getTweet() {
return tweet;
}
public void setTweet(String tweet) {
this.tweet = tweet;
}
}
TweetRepository.java
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TweetRepository extends JpaRepository<Tweet, Integer>{
public Tweet findAllById(Integer userid);
}
TwitterController.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class TwitterController{
@Autowired
private UserRepository userRepo;
private TweetRepository tweetRepo;
@RequestMapping("/")
public String index(Model model) {
//model.addAttribute("message", "Welcome to spring!");
return "index";
}
@RequestMapping("/testing")
public String testing(Model model) {
model.addAttribute("message", "Welcome to spring boot!");
return "testing";
}
@GetMapping("/register")
public String signup(Model model) {
User user = new User();
model.addAttribute("user", user);
return "register";
}
@RequestMapping("/login")
public String login(Model model) {
return "login";
}
@RequestMapping("/tweeting")
public String tweeting(Model model) {
Tweet tweet = new Tweet();
model.addAttribute("tweet", tweet);
return "tweeting";
}
@PostMapping("/process_register")
public String processRegister(User user) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encodedPassword = encoder.encode(user.getPassword());
user.setPassword(encodedPassword);
userRepo.save(user);
return "register_success";
}
@PostMapping("/process_tweet")
public String processTweet(Tweet tweet) {
tweetRepo.save(tweet);
return "tweet_success";
}
}
tweeting.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Tweet</title>
<link href="webjars/bootstrap/5.1.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container col-xl-10 col-xxl-8 px-4 py-5">
<div class="row align-items-center g-lg-5 py-5">
<div class="col-md-10 mx-auto col-lg-5">
<form class="p-4 p-md-5 border rounded-3 bg-light" th:action="@{/process_tweet}" th:object="${tweet}" method="post">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="floatingInput" placeholder=tweet th:field="*{tweet}">
<label for="floatingInput">Tweet</label>
</div>
<button class="w-100 btn btn-lg btn-info" type="submit">Post</button>
</form>
</div>
</div>
</div>
<script src="webjars/jquery/3.6.0/jquery.min.js"></script>
<script src="webjars/bootstrap/5.1.0/js/bootstrap.min.js"></script>
<script>
</script>
</body>
</html>
I'm attempting to build a twitter clone website using spring boot. I'm getting an error and looked through my code. I'm not sure what I'm doing wrong:
2022-03-15 12:23:51.067 WARN 4580 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'com.example.demo.Tweet'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'Test'; nested exception is java.lang.NumberFormatException: For input string: "Test"]
This is a springboot application using postgres as its database. I also have a User repository in the code that does sign me in properly. I would appreciate any assistance with this. Thank you.
Edit: Added full stacktrace.
Edit2: Added an image of the tweeting.html to show where the string "Test" came from.
Tweet.java
package com.example.demo;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
@Entity
@Table(name="tweets")
public class Tweet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id",unique=true, nullable = false)
private Integer id;
@Column(name="userid", unique=true, nullable=false)
private Integer userid;
@Column(name="tweet", nullable=false)
private String tweet;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userid;
}
public void setUserId(Integer userid) {
this.userid = userid;
}
public String getTweet() {
return tweet;
}
public void setTweet(String tweet) {
this.tweet = tweet;
}
}
TweetRepository.java
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TweetRepository extends JpaRepository<Tweet, Integer>{
public Tweet findAllById(Integer userid);
}
TwitterController.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class TwitterController{
@Autowired
private UserRepository userRepo;
private TweetRepository tweetRepo;
@RequestMapping("/")
public String index(Model model) {
//model.addAttribute("message", "Welcome to spring!");
return "index";
}
@RequestMapping("/testing")
public String testing(Model model) {
model.addAttribute("message", "Welcome to spring boot!");
return "testing";
}
@GetMapping("/register")
public String signup(Model model) {
User user = new User();
model.addAttribute("user", user);
return "register";
}
@RequestMapping("/login")
public String login(Model model) {
return "login";
}
@RequestMapping("/tweeting")
public String tweeting(Model model) {
Tweet tweet = new Tweet();
model.addAttribute("tweet", tweet);
return "tweeting";
}
@PostMapping("/process_register")
public String processRegister(User user) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encodedPassword = encoder.encode(user.getPassword());
user.setPassword(encodedPassword);
userRepo.save(user);
return "register_success";
}
@PostMapping("/process_tweet")
public String processTweet(Tweet tweet) {
tweetRepo.save(tweet);
return "tweet_success";
}
}
tweeting.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Tweet</title>
<link href="webjars/bootstrap/5.1.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container col-xl-10 col-xxl-8 px-4 py-5">
<div class="row align-items-center g-lg-5 py-5">
<div class="col-md-10 mx-auto col-lg-5">
<form class="p-4 p-md-5 border rounded-3 bg-light" th:action="@{/process_tweet}" th:object="${tweet}" method="post">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="floatingInput" placeholder=tweet th:field="*{tweet}">
<label for="floatingInput">Tweet</label>
</div>
<button class="w-100 btn btn-lg btn-info" type="submit">Post</button>
</form>
</div>
</div>
</div>
<script src="webjars/jquery/3.6.0/jquery.min.js"></script>
<script src="webjars/bootstrap/5.1.0/js/bootstrap.min.js"></script>
<script>
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论