[英]Spring @Value — Failed to convert value of type 'java.lang.String' to required type 'int' [on hold]
[英]Spring Boot Get Request: Failed to convert type java.lang.String to required type int
我正在使用帶有 PagingAndSortingRepository 的 Spring 引導。
我的帖子實體:
@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 請求帖子,使用此 URL: 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"]
問題是自動生成的方法會自動獲取 /search 前綴。
答案: http://localhost:8081/posts/search/byAuthorId?page=1&size=1&authorId=1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.