使用Spring引导的自定义JSON响应
我开发了一个spring引导应用程序,在控制器中我有一个返回产品列表的方法使用Spring引导的自定义JSON响应,json,spring,spring-boot,spring-rest,Json,Spring,Spring Boot,Spring Rest,我开发了一个spring引导应用程序,在控制器中我有一个返回产品列表的方法 @GetMapping public ResponseEntity<List<Product>> listAllProducts() { List<Product> products = productRepository.findAll(); if (products.isEmpty()) { return new ResponseEntity<
@GetMapping
public ResponseEntity<List<Product>> listAllProducts() {
List<Product> products = productRepository.findAll();
if (products.isEmpty()) {
return new ResponseEntity<List<Product>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Product>>(products, HttpStatus.OK);
}
如何修改方法listAllProducts,使结果如下
{
"products": [
{
"name": "Ruby on Rails Baseball Jersey",
"description": null,
"price": 19.99,
"slug": "ruby-on-rails-tote"
},
{
"name": "Ruby on Rails Baseball Jersey",
"description": null,
"price": 19.99,
"slug": "ruby-on-rails-tote"
},
{
"name": "Ruby on Rails Baseball Jersey",
"description": null,
"price": 19.99,
"slug": "ruby-on-rails-tote"
}
]
}
一种简单的方法是使用
ProductListDto
包含List
公共类ProductListDto{
私人上市产品;
public ProductListDto(){}
公共产品列表DTO(列出产品){
这一点。产品=产品;
}
}
然后:
@GetMapping
public ResponseEntity<ProductListDto> listAllProducts() {
List<Product> products = productRepository.findAll();
if (products.isEmpty()) {
return new ResponseEntity<ProductListDto>(HttpStatus.NO_CONTENT);
}
ProductListDto productListDto = new ProductListDto(products);
return new ResponseEntity<ProductListDto>(productListDto, HttpStatus.OK);
}
@GetMapping
公共责任清单所有产品(){
List products=productRepository.findAll();
if(products.isEmpty()){
返回新的响应属性(HttpStatus.NO_内容);
}
ProductListDto ProductListDto=新产品ListDTO(产品);
返回新的响应属性(productListDto,HttpStatus.OK);
}
一种简单的方法是使用ProductListDto
包含列表
公共类ProductListDto{
私人上市产品;
public ProductListDto(){}
公共产品列表DTO(列出产品){
这一点。产品=产品;
}
}
然后:
@GetMapping
public ResponseEntity<ProductListDto> listAllProducts() {
List<Product> products = productRepository.findAll();
if (products.isEmpty()) {
return new ResponseEntity<ProductListDto>(HttpStatus.NO_CONTENT);
}
ProductListDto productListDto = new ProductListDto(products);
return new ResponseEntity<ProductListDto>(productListDto, HttpStatus.OK);
}
@GetMapping
公共责任清单所有产品(){
List products=productRepository.findAll();
if(products.isEmpty()){
返回新的响应属性(HttpStatus.NO_内容);
}
ProductListDto ProductListDto=新产品ListDTO(产品);
返回新的响应属性(productListDto,HttpStatus.OK);
}
创建模型类
public class Products {
private List<Product> products = null;
}
公共类产品{
私有列表产品=空;
}
并修改您的回复以退回产品,而不是列表
public ResponseEntity<Products> listAllProducts()
public ResponseEntity listAllProducts()
这将为您解决问题创建一个模型类
public class Products {
private List<Product> products = null;
}
公共类产品{
私有列表产品=空;
}
并修改您的回复以退回产品,而不是列表
public ResponseEntity<Products> listAllProducts()
public ResponseEntity listAllProducts()
这应该可以为您解决问题您能将ResponseEntity更改为ResponseEntity并使列表成为MyProductList的成员吗?您能将ResponseEntity更改为ResponseEntity并使列表成为MyProductList的成员吗?@aymenkn,如果findAll()提供空列表,我将返回HttpStatus.OK和空列表。供将来参考:标题为“除非您为ProductListDto提供公共getter,否则ResponseEntity将无法构建JSON”:此编辑旨在针对文章作者,作为编辑毫无意义。它应该作为注释或答案编写。@aymenkn,我将返回HttpStatus.OK,如果findAll()则返回空列表提供一个空列表。供将来参考:标题为“除非您为ProductListDto提供公共getter,否则ResponseEntity将无法构建JSON”:此编辑旨在针对文章作者,作为编辑没有任何意义。它应该作为评论或回答编写。