【发布时间】:2020-08-04 07:49:21
【问题描述】:
我正在使用带有 PagingAndSortingRepository 的 Spring Boot。
我的帖子实体:
@Setter
@Getter
@Entity
@NoArgsConstructor
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotNull
private int branch;
@NotNull
@Size(min = 240, max = 10000)
private String article;
@NotNull
private String featuredImage;
@NotNull
private int authorId;
private Date datePublished = new Date();
private boolean commentsEnabled = true;
private boolean enabled = false;
private int views;
private String snippetTitle;
private String snippetDescription;
@ManyToMany
Set<Category> categories;
@Transient
private Set<Comment> comments;
public Post(int branch , String article, String featuredImage, int authorId){
this.branch = branch;
this.article = article;
this.featuredImage = featuredImage;
this.authorId = authorId;
}
我的帖子存储库:
public interface PostRepository extends PagingAndSortingRepository<Post, Integer> {
@RestResource(path = "/byAuthorId")
Page<Post> getPostsByAuthorId (Pageable pageable, int authorId);
@Override
@RestResource(exported = false)
Page<Post> findAll(Pageable pageable);
@Override
@RestResource(exported = false)
Optional<Post> findById(Integer integer);
}
我正在尝试使用 getPostsByAuthorId 方法通过 authorId 请求帖子,网址为:http://localhost:8081/posts/byAuthorId?page=1&size=1&authorId=1
不幸的是,我不断收到此错误:
Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "byAuthorId"]
- 我已经尝试添加 @RequestParam("authorId") int authorId。
- 我已经尝试重写方法来检查 Intellij 是否仍然提供 getPostsByAuthorId 作为方法,而不是像 getPostsByAuthor_Id 这样的方法
- 我没有对控制器做任何关于这个方法的事情。
【问题讨论】:
标签: java hibernate spring-boot