【问题标题】:How to return list dto when using select new使用select new时如何返回列表dto
【发布时间】:2019-05-14 14:33:18
【问题描述】:

我使用 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;
}

控制器。

@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&lt;CategoryResDto> 因为我想要自定义一些消息返回

【问题讨论】:

  • 你最好直接返回一个List&lt;CategoryResDto&gt;
  • CategoryResDtoList != CategoryResDto
  • 因为我想在返回时重命名某些字段。示例:当我使用时,我想重命名 listCategory 返回名称为“category_name”:@Jsonproperty。可能返回 CategoryResDtoList ?
  • 你试过在CategoryResDtoList上使用@NoArgsConstructor吗?

标签: java spring hibernate spring-boot jpa


【解决方案1】:

不返回CategoryResList,直接返回List&lt;CategoryResDto&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2013-12-30
    • 2019-10-04
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多