简体   繁体   English

Spring 引导获取请求:无法将类型 java.lang.String 转换为所需的 int 类型

[英]Spring Boot Get Request: Failed to convert type java.lang.String to required type int

I am using Spring boot with the PagingAndSortingRepository.我正在使用带有 PagingAndSortingRepository 的 Spring 引导。

My Post entity:我的帖子实体:

@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;
    }

My PostRepository:我的帖子存储库:

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);
}

I am trying to request the Posts by authorId with the getPostsByAuthorId method, with this URL: http://localhost:8081/posts/byAuthorId?page=1&size=1&authorId=1我正在尝试使用 getPostsByAuthorId 方法通过 authorId 请求帖子,使用此 URL: http://localhost:8081/posts/byAuthorId?page=1&size=1&authorId=1

Unfortunately I keep getting this error:不幸的是,我不断收到此错误:

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"]
  • I already tried adding @RequestParam("authorId") int authorId.我已经尝试添加 @RequestParam("authorId") int authorId。
  • I already tried re-writing the method to check if Intellij still provides getPostsByAuthorId as method and not something like getPostsByAuthor_Id我已经尝试重新编写方法来检查 Intellij 是否仍然提供 getPostsByAuthorId 作为方法,而不是像 getPostsByAuthor_Id 这样的方法
  • I haven't done anything with the controller regarding this method.关于这种方法,我没有对 controller 做任何事情。

The problem was that autogenerated methods automatically get /search prefix.问题是自动生成的方法会自动获取 /search 前缀。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring @Value —无法将类型“ java.lang.String”的值转换为所需的类型“ int” [保留] - Spring @Value — Failed to convert value of type 'java.lang.String' to required type 'int' [on hold] spring 无法将“java.lang.String”类型的值转换为所需的“java.lang.reflect.Type”类型 - spring failed to convert value of type 'java.lang.String' to required type 'java.lang.reflect.Type' Spring Boot 2.1.5 无法将 java.lang.String 类型的属性值转换为所需类型 java.time.LocalDate - Spring Boot 2.1.5 Failed to convert property value of type java.lang.String to required type java.time.LocalDate Spring NumberFormatException:无法将类型&#39;java.lang.String&#39;的值转换为所需的类型&#39;java.lang.Long - Spring NumberFormatException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long 无法在 SPRING 中将类型“java.lang.String”的值转换为所需的类型“java.lang.Long” - Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long' in SPRING 在JPA Spring中无法将类型&#39;java.lang.String&#39;的值转换为所需的类型&#39;java.lang.Integer&#39; - Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer' In jpa spring 无法将“java.lang.String”类型的属性值转换为属性“id”所需的类型“int” - Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'id' 发送 GET 请求时无法将“java.lang.String”类型的值转换为所需类型“java.util.Date” - Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' when sending GET request Spring/Thymeleaf - 无法将类型“java.lang.String”的值转换为所需类型 - Spring/Thymeleaf - Failed to convert value of type 'java.lang.String' to required type 无法将“ java.lang.String”的值转换为所需的类型“ myEnum” - Failed to convert value of 'java.lang.String' to required type 'myEnum'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM