Category.java
@Entity
@Data
@Table(name = "categories")
public class Category implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Size(min = 5, max = 60)
@Column(name = "category", length = 50, unique = true)
private String category;
@OneToMany
private Set<Product> product;
}
类别Controller.java
@GetMapping("/category/{id}")
public ResponseEntity<List<Product>> getCategoryById(@PathVariable(value = "id") Long categoryId) {
List<Product> category = productRepository.findAllByCategoryId(categoryId);
if (category == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok().body(category);
}
ControllerRepositoy.java
@Repository
public interface CategoryRepository extends JpaRepository<Category,Long>{
}
Products.Java
@Entity
@Data
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Size(min = 5, max = 60)
@Column(name = "productName", length = 50, unique = true, nullable = false)
private String productName;
@NotNull
@Size(min = 10, max = 200)
@Column(name = "description", length = 200, unique = true, nullable = false)
private String description;
@NotNull
//@Pattern(regexp = "^[0-9]*$")
@Size(min = 1, max = 10)
@Column(name = "price", length = 10, unique = true, nullable = false)
private int price;
@ManyToOne
@JoinColumn(name="product")
private Category categoryId;
@NotNull
@Column(name = "imageURL", length = 500, nullable = false)
private String image;
}
ProductController.java
@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
ProductRepository productRepository;
@GetMapping("/product")
public List<Product> getAllNotes() {
return productRepository.findAll();
}
@GetMapping("/product/{id}")
public ResponseEntity<Optional<Product>> getProductById(@PathVariable(value = "id") Long categoryID) {
Optional<Product> product = productRepository.findById(categoryID);
if(product == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok().body(product);
}
@PostMapping("/product")
public @Valid Product createProduct(@Valid @RequestBody Product product) {
System.err.println(product);
return productRepository.save(product);
}
}
我想使用类别ID查询产品。所以我在categoryController中使用了productRepository。
这是代码。当为GET请求提供Id时,它会显示错误。错误是:
java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [com.susl.Agroapi.model.Category (n/a)]
如何解决?
答案 0 :(得分:1)
您必须更改以下代码
将产品实体添加到
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
private Category category;
将类别实体添加到
@OneToMany(mappedBy="category",cascade=CascadeType.REMOVE)
private Set<Product> pr;
制作如下所示的产品库
public interface ProductRepository extends PagingAndSortingRepository<Product, Long>{
List<Product> findAllProductByCategoryId(@Param("id")Long id);
}
然后,您可以使用带有findAllProductByCategoryId({categoryId})
答案 1 :(得分:0)
您仍未发布请求的ProductRepository,但现在我们至少拥有了Product实体,错误很可能是该方法应该命名
findAllByCategoryIdId
因为您想要按字段id
的{{1}}进行搜索。
或者你可以(而且应该)将字段categoryId
重命名为categoryId
,因为它就是这样的:一个类别,而不是一个ID。
答案 2 :(得分:-1)
看起来您想要使用CategoryRepository并使用ProductRepository。
创建:
@Autowired
CategoryRepository categoryRepository;