gpt4 book ai didi

java - 使用 select new 时如何返回列表 dto

转载 作者:行者123 更新时间:2023-12-02 05:41:43 28 4
gpt4 key购买 nike

我使用 spring boot 并使用 select new return dto 因为我不想返回我的实体。但是当我返回列表时它不起作用。它引发了我的异常:

 "timestamp": "2019-05-14T14:06:06.762+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No converter found capable of converting from type [com.abc.demospringjpa.dto.CategoryResDto] to type [com.abc.demospringjpa.model.CategoryResDtoList]",

类别模型。

package com.abc.demospringjpa.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
//@RequiredArgsConstructor
public class Category implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String type;
}

Controller 。

@RestController
@RequestMapping("/api/v1/categories")
public class CategoryController {

@Autowired
private CategoryService categoryService;
//
// @GetMapping
// public ResponseEntity<?> getAllCategory() {
// return new ResponseEntity<>(categoryService.findAllCategory(), HttpStatus.OK);
// }

@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public CategoryResDto findCategoryByName(@PathVariable("id")Long id) {
return categoryService.findCategoryByName(id);
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<?> findCategoryByName(@RequestParam("type")String type) {
return new ResponseEntity<>(categoryService.findCategoryByType(type), HttpStatus.OK);
}
//
// @GetMapping
// public ResponseEntity<?> saveCategory() {
// return new ResponseEntity<>(categoryService.saveCa(), HttpStatus.OK);
// }
}

服务

public interface CategoryService {
List<CategoryResDto> findAllCategory();
CategoryResDto findCategoryByName(Long name);
CategoryResDtoList findCategoryByType(String type);
// CategoryResDto saveCategory(Category category);
}

服务实现

@Service
public class CategoryServiceImpl implements CategoryService {

final
CategoryRepository categoryRepository;

final
CategoryMapper categoryMapper;

@Autowired
public CategoryServiceImpl(CategoryRepository categoryRepository, CategoryMapper categoryMapper) {
this.categoryRepository = categoryRepository;
this.categoryMapper = categoryMapper;
}

@Override
public List<CategoryResDto> findAllCategory() {
return categoryRepository.findAll().stream().
map(categoryMapper::categoryToCategoryDto).collect(Collectors.toList());
}

@Override
public CategoryResDto findCategoryByName(Long id) {
return categoryRepository.findAllCategoryById(id);
}

@Override
public CategoryResDtoList findCategoryByType(String type) {
return categoryRepository.findAllCategoryResponseDto(type);
}
}

存储库。

@Repository
public interface CategoryRepository extends JpaRepository<Category,Long> {

@Query(value = "Select new com.abc.demospringjpa.dto.CategoryResDto(c.name) from Category c where c.id = :id")
CategoryResDto findAllCategoryById(@Param("id") Long id);

@Query(value = "Select new com.abc.demospringjpa.dto.CategoryResDto(c.name) from Category c where c.type = :type")
CategoryResDtoList findAllCategoryResponseDto(@Param("type")String type);
}

类别资源:

 @Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@RequiredArgsConstructor
//@NoArgsConstructor
@AllArgsConstructor
public class CategoryResDto {

@JsonProperty("category_name")
private String name;
}





CategoryResList:
@RequiredArgsConstructor
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CategoryResDtoList {

private List<CategoryResDto> categoryResDtos;
}

我想返回 CategoryResDtoList 因为它包含 CategoryResDto 。有可能吗?当我执行并调用方法时,它抛出异常:

 "timestamp": "2019-05-14T14:06:06.762+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No converter found capable of converting from type [com.abc.demospringjpa.dto.CategoryResDto] to type [com.abc.demospringjpa.model.CategoryResDtoList]",

我有一个问题:如何使用 Select new 返回我的 DTO CategoryResDtoList List<CategoryResDto > 因为我想要自定义一些消息返回

最佳答案

而不是返回 CategoryResList ,返回List<CategoryResDto>直接。

关于java - 使用 select new 时如何返回列表 dto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56132898/

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