You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Rest API中如何让JPA Repository findAll()仅返回数值数组

解决方案:将JPA查询结果转为纯字符串数组JSON响应

你当前用findAll()返回的是实体对象列表,Jackson默认会把每个实体序列化为包含Currency字段的JSON对象,所以响应是对象数组。要得到纯字符串数组,有两种常用实现方式:

方式一:在Repository层直接查询目标字段

直接通过JPA查询提取Currency字段的值,避免返回整个实体对象,效率更高。

1. JPA Repository代码

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;

public interface CurrencyRepository extends JpaRepository<CURRIENCIES_TABLE, Long> {
    // 自定义查询,只获取所有Currency字段的值
    @Query("SELECT c.currency FROM CURRIENCIES_TABLE c")
    List<String> findAllCurrencyValues();
}

2. Service Impl代码

import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CurrencyServiceImpl {
    private final CurrencyRepository currencyRepository;

    // 构造注入(推荐替代@Autowired)
    public CurrencyServiceImpl(CurrencyRepository currencyRepository) {
        this.currencyRepository = currencyRepository;
    }

    public List<String> getAllCurrencyValues() {
        return currencyRepository.findAllCurrencyValues();
    }
}

3. Controller代码

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/api/currencies")
public class CurrencyController {
    private final CurrencyServiceImpl currencyService;

    public CurrencyController(CurrencyServiceImpl currencyService) {
        this.currencyService = currencyService;
    }

    @GetMapping
    public ResponseEntity<List<String>> getAllCurrencies() {
        List<String> currencies = currencyService.getAllCurrencyValues();
        return ResponseEntity.ok(currencies);
    }
}

方式二:在Service层转换实体列表

如果不想修改Repository结构,可在Service层通过Stream流把实体列表映射为字符串列表。

修改后的Service Impl代码

import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CurrencyServiceImpl {
    private final CurrencyRepository currencyRepository;

    public CurrencyServiceImpl(CurrencyRepository currencyRepository) {
        this.currencyRepository = currencyRepository;
    }

    public List<String> getAllCurrencyValues() {
        // 从实体列表中提取Currency字段值
        return currencyRepository.findAll()
                .stream()
                .map(CURRIENCIES_TABLE::getCurrency) // 调用实体类的getter方法
                .collect(Collectors.toList());
    }
}

实体层CURRIENCIES_TABLE.java注意事项

确保实体类有正确的字段映射和getter方法:

import jakarta.persistence.*; // Spring Boot 3+用jakarta,旧版本用javax.persistence

@Entity
@Table(name = "CURRIENCIES_TABLE")
public class CURRIENCIES_TABLE {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "Currency") // 匹配数据库列名
    private String currency;

    // 无参构造器(JPA强制要求)
    public CURRIENCIES_TABLE() {}

    // getter方法
    public String getCurrency() {
        return currency;
    }

    // 可选:setter和全参构造器
}

两种方式最终都会让接口返回["Rupee","Dollar","Euro","Pound"]格式的JSON响应。

内容的提问来源于stack exchange,提问作者Harishreddy2854

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 16:01:48