我正在为大学编写一个小程序。我的任务是编写一个可以执行CRUD操作的java spring Rest应用程序。我不必制作显示查询结果的表单,如果我用 postman 或休息来测试它就可以了。
我正在创建一家书店,当然每本书都有一个唯一的UUID。这是我的 Controller 文件。
package controllers;
import bookstore.exceptions.DateIsToLateException;
import bookstore.exceptions.NoMatchingIDException;
import bookstore.models.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import services.BookService;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import static java.awt.PageAttributes.MediaType.A;
@RestController
public class BookController {
private BookService service;
public BookController(@Autowired BookService service) {
this.service = service;
}
@RequestMapping(value = "/count", method = RequestMethod.GET)
@ResponseBody
public String showNumberOfBooks(){
return String.valueOf(service.getBooks().size());
}
@RequestMapping(value = "/books", method = RequestMethod.GET)
@ResponseBody
public Collection<Book> getAllBook(){
return service.getBooks();
}
@RequestMapping(value = "/books/add", method = RequestMethod.POST)
@ResponseBody
public Book addNewBook(@RequestBody Book book) throws DateIsToLateException, NoMatchingIDException {
service.addBook(book);
return service.getBook(book.getId());
}
@RequestMapping(value = "/books/title/{title}", method = RequestMethod.GET)
@ResponseBody
public Collection<Book> getBookbyTitle(@PathVariable String title){
Collection<Book> books= service.getBook();
Collection<Book> result = new ArrayList<Book>();
for (Book b: books){
if(b.getTitle().equalsIgnoreCase(title)){
result.add(b);
}
}
return result;
}
@RequestMapping(value = "/books/genre/{genre}", method = RequestMethod.GET)
@ResponseBody
public Collection<Book> getBookByGenre(@PathVariable String genre){
Collection<Book> books= service.getBooks();
Collection<Book> result = new ArrayList<Book>();
for (Book b: books){
if(b.getGenre().toString().equalsIgnoreCase(genre)){
result.add(b);
}
}
return result;
}
@RequestMapping(value = "books/{id}", method = RequestMethod.GET)
@ResponseBody
public Book getBookById(@PathVariable UUID id) throws NoMatchingIDException {
return service.getBook(id);
}
@ExceptionHandler(NoMatchingIDException.class)
@ResponseBody
public String handleNoMatchingIDException(Exception e){
return "Corresponding UUID does not exist. Maybe a typo? " + e.getMessage();
}
@ExceptionHandler(DateIsToLateException.class)
@ResponseBody
public String handleDateIsToLateException(Exception e){
return "Date is invalid! Maybe the date you've entered is after 1 years of today's date? " + e.getMessage();
}
@RequestMapping(value = "/books/delete/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String deleteBook(@PathVariable UUID id) throws NoMatchingIDException {
service.deleteBook(id);
return "Deleting succesful";
}
@RequestMapping(value="/books/year/{year}", method = RequestMethod.GET)
@ResponseBody
public Collection<Book> getBooksFromYear(@PathVariable int year){
Collection<Book> books = service.getBooks();
Collection<Book> thisYearBooks = new ArrayList<Book>();
for (Book b: books){
if(b.getYearofPublication().getYear() == year){
thisYearBooks.add(b);
}
}
return thisYearBooks;
}
@RequestMapping(value = "/books/{id}", method = RequestMethod.PUT)
@ResponseBody
public Book updateBook(@PathVariable UUID id, @RequestBody Book book) throws DateIsToLateException, NoMatchingIDException {
book.setId(id);
service.updateBook(book, id);
return book;
}
}
例如,您可以通过 id 发出对书籍的 GET 请求,如下所示: http:localhost:8080/books/{这里有一个很长的 UUID}
我的问题是,如果我输入了错误的、不存在的 UUID,我可以处理,但如果我尝试在 UUID 末尾附加 4-5 个字符,我不会收到任何异常,但程序仍然崩溃。如果发生这种情况,我想制作一个类似“UUID 字符太多”之类的文本。有什么办法可以解决这个问题吗?
最佳答案
试试这个:
@RequestMapping(value = "books/{id}", method = RequestMethod.GET)
@ResponseBody
public Book getBookById(@PathVariable("id") @NotBlank @Size(max = 10, message="Too many characters for an UUID") UUID id) throws NoMatchingIDException {
return service.getBook(id);
}
关于java - 是否有异常(exception),可以让我处理用户尝试为 UUID 输入过多字符的情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59219127/