作者think12381 ()
看板java
标题[问题]spring boot 报错 NumberFormatException
时间Thu Dec 12 22:32:57 2019
请问一下各位大大 我目前正在学习Java spring boot
开发环境是采 用 idea
目的是要采用网页去DB里面捞资料出来
都会产生以下错误
Failed to convert value of type 'java.lang.String' to required type 'long';
nested exception is java.lang.NumberFormatException: For input string: "books"
看起来是说 自串不能转long
但是我这样要怎麽对到 html去
google许久还是查不到解法
请教各位大神
谢谢
以下是局部代码
实体
===========================================
@Entity
public class Book2 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private int status;
private String author;
private String description;
===============================================
controller
@GetMapping("/booklist/{id}")
public String ListBook(@PathVariable("id") long id, Model model){
Optional<Book2> book2 = bookService.getOne(id);
if(book2 == null){
book2 = Optional.of(new Book2());
}
model.addAttribute("book", book2);
System.out.println(book2.get().getName()+ " "
+
book2.get().getId() + " "+
book2.get().getAuthor()+ " "+
book2.get().getDescription()+ " "+
book2.get().getStatus());
return "books";
=============================================================================
book repository
public interface BookRepository extends JpaRepository<Book2,Long> {
==============================================================================
service
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
===========================================================================
books.html
<body>
<div class = "container" style="max-width: 700px">
<h2 class = "page-header" style="margin-top: 50px">书单内容</h2>
<div class="well">
<p>
<strong> 作者 </strong>
<span th:text = "${book.author}"> bb
</span>
</p>
<p>
<strong> 描述 </strong>
<span th:text = "${book.description}"> cc </span>
</p>
<p>
<strong> 书名 </strong>
<span th:text = "${book.name}"> aa </span>
</p>
<p>
<strong> 状态 </strong>
<span th:text = "${book.status}"> dd </span>
</p>
</div>
</div>
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.141.140.205 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/java/M.1576161180.A.50C.html
1F:→ ssccg: 你是连什麽网址? 12/12 23:09
2F:→ think12381: localhost:8080/booklist/4 12/13 06:57
3F:→ think12381: 4是我在db要抓的资料 12/13 06:57
4F:推 kohmiho: 感觉像dbo 栏位没对准 12/13 08:20
5F:→ ssccg: 你是不是用成@RestController? 12/13 09:19
6F:→ think12381: entity 栏位 顺序 要一致? 12/13 09:48
7F:→ tw11509: 有stack的话,一起给出来,比较好帮你确认问题在哪里 12/13 11:11
8F:→ tw11509: 而且你book2 == null写法有问题,已经用optional了,先 12/13 11:13
9F:→ tw11509: 去看看optional的用法 12/13 11:13
10F:→ tw11509: 你放入model的是optional物件,不是book2物件,要先从op 12/13 11:16
11F:→ tw11509: tional去get出来 12/13 11:16
12F:推 tw11509: 你的pathvariable应该不是带入books吧 12/13 11:40
13F:推 bloodrance: .findById(id).orElse(null); < Service那边要写个 12/24 16:24
14F:→ bloodrance: 没记错当时我用Spring 2.0看1.5的教学时发生过 12/24 16:24
15F:→ bloodrance: 不过你的写法我还是觉得怪诡异的 spring boot版本是? 12/24 16:31
16F:推 chi972121: Optional有isPeesent 12/25 20:57
17F:→ chi972121: *isPresent 12/25 20:57
18F:推 wxv1990: book2.isEmpty 03/08 15:58