【问题标题】:SpringBoot : RestAPI display the unique recordSpring Boot:Rest API 显示唯一记录
【发布时间】:2020-04-09 18:22:57
【问题描述】:

表: this is table name

要求:

使用 RestAPI 调用填充唯一的 cateID, categoryName ,但我得到了整个记录。

表格

Table Data which I am using

代码说明:

存储库:

@Repository
public interface CategoryRepository extends JpaRepository<xCategory,Integer>
{
    // @Query("SELECT DISTINCT a.catID,a.categoryName FROM  ccCategory a order by categoryName asc")
    @Query("SELECT DISTINCT a.catID, a.categoryName FROM xCategory a order by categoryName asc")
    List<ccCategory> getCategoryName();
}

休息控制器:

@RestController
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
public class HomeResource {

    private final Logger log = LoggerFactory.getLogger(HomeResource.class);

    @Autowired
    CategoryRepository categoryRepository;



@GetMapping("/getAllCategory")
    public List<ccCategory> getAllCategory() {
        // public List<String> getAllCategory() {
        System.out.println("***** Call : API getAllCategory() ******");

        List<ccCategory> cCategory = categoryRepository.findAll();
        return cCategory;
    }

角度代码:

<label class="control-label">Category: </label>
                <select  [(ngModel)]="listAllCategory" name="xxcategory" class="form-control" required>
                    <option *ngFor="let xxcategory of listAllCategory" [value]="xxcategory.catID">
                        {{xxcategory.categoryName}}
                    </option>
                </select>

问题: 下拉填充所有表值,但我只想要唯一值,例如一次 catID , categoryName。

【问题讨论】:

  • API 填充唯一数据对吗?
  • 开始获取唯一数据 .... 仅在存储库中更改
  • ABHI 你是对的,但我的数据没有以 JSON 格式填充....任何具体原因它以 [[1,"A"],[2,"V"]
  • 您检查过 RestAPI App 中的任何错误吗?

标签: angularjs spring-boot spring-boot-actuator


【解决方案1】:

您必须添加一个变量以将所选元素保留在您的 select 中,然后将您的 [(ngModel)]="listAllCategory" 更改为 [(ngModel)]="selectedCategory"

【讨论】:

  • Angular 方面我得到的数据不是问题。
猜你喜欢
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 2021-02-03
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多