我有一个REST端点,可以调用该端点来请求一些数据,但是它在JSON响应中返回了3次相同的对象,而不是仅返回不同的结果。这是我得到的答复:[ { "id": "5555 ", "label": "MAIN USER (5555) " }, { "id": "5555 ", "label": "MAIN USER (5555) " }, { "id": "5555 ", "label": "MAIN USER (5555) " }]这是用于生成JSON响应的控制器:@RestControllerpublic class AutoCompleteController { private AutoCompleteService autoCompleteService; private EntityManager em; public AutoCompleteController(AutoCompleteService autoCompleteService, EntityManager em) { this.autoCompleteService = autoCompleteService; this.em = em; } @RequestMapping(value = "jobs", method = RequestMethod.GET) public @ResponseBody List<AutoComplete> getSalary(@RequestParam(value = "autocomplete") String autocompleteValue) { return autoCompleteService.retrieveSalary(autocompleteValue); }}依次,控制器调用AutoCompleteService:@Servicepublic class AutoCompleteService { private AutoCompleteRepository autocompleteRepository; public AutoCompleteService(AutoCompleteRepository autRepo) { this.autocompleteRepository = autRepo; } public List<AutoComplete> retrieveSalary(String jobClassCd) { List<AutoComplete> salaries = autocompleteRepository.findAllByJobClassCdIsContaining(jobClassCd); if (salaries.size() <= 0) { throw new AutoCompleteNotFoundException(jobClassCd); } return salaries; }} 最佳答案 只要确保您的方法autoCompleteService.retrieveSalary仅返回1个结果,就根据您的过滤器检查数据库是否存在内容相同的多行。如果您始终希望得到1个结果,则使您返回一个Object,而不是List 。
10-04 12:15