gpt4 book ai didi

java - Spring Boot 通过 Id 请求数据

转载 作者:搜寻专家 更新时间:2023-11-01 03:46:45 25 4
gpt4 key购买 nike

分类.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>{

}

产品.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)]

如何解决?

最佳答案

你必须像下面这样更改代码

将产品实体添加到

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

然后您可以通过 Controller 类调用 ProductRepository 和 findAllProductByCategoryId({categoryId})

关于java - Spring Boot 通过 Id 请求数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48507081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com