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

时间:2019-05-14 14:33:19

标签: java spring hibernate spring-boot jpa

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

ServiceImpl

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

CategoryRes:

 @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>,因为我想自定义一些消息返回

1 个答案:

答案 0 :(得分:0)

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