【问题标题】:Spring Boot Get Request: Failed to convert type java.lang.String to required type intSpring Boot 获取请求:无法将 java.lang.String 类型转换为所需的 int 类型
【发布时间】: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


    【解决方案1】:

    问题在于自动生成的方法会自动获取 /search 前缀。

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

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 2021-03-13
      • 1970-01-01
      • 2014-02-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 2021-09-02
      相关资源
      最近更新 更多