簡體   English   中英

Spring 引導獲取請求:無法將類型 java.lang.String 轉換為所需的 int 類型

[英]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"]
  • 我已經嘗試添加 @RequestParam("authorId") int authorId。
  • 我已經嘗試重新編寫方法來檢查 Intellij 是否仍然提供 getPostsByAuthorId 作為方法,而不是像 getPostsByAuthor_Id 這樣的方法
  • 關於這種方法,我沒有對 controller 做任何事情。

問題是自動生成的方法會自動獲取 /search 前綴。

答案: http://localhost:8081/posts/search/byAuthorId?page=1&size=1&authorId=1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM