gpt4 book ai didi

java - 如何通过java中的搜索正确请求与请求参数的映射?

转载 作者:行者123 更新时间:2023-12-02 09:47:01 25 4
gpt4 key购买 nike

我遇到一个问题,我试图使用请求参数请求 map ,基本上我需要对此网址 jobs?autocomplete=0100 执行 GET 请求,并且由于某种原因我得到我所有的 /jobs 相反,你能帮我解决这个问题吗?谢谢!

@RestController
//@RequestMapping(value = "/jobs", params = "autocomplete")
public class AutoCompleteController {

private AutoCompleteService autoCompleteService;

private EntityManager em;

public AutoCompleteController(AutoCompleteService autoCompleteService, EntityManager em) {

this.autoCompleteService = autoCompleteService;
this.em = em;
}


@GetMapping(value = "/jobs", produces = "application/json")
public List<AutoComplete> getSalary(
@RequestParam(name= "autocomplete", required = true) String autocomplete,
@PathVariable("jobClassCd") String jobClassCd) {

return autoCompleteService.retrieveSalary(jobClassCd);

}





public void getAllSalaries(HttpServletResponse res, HttpServletRequest req) {

Stream<AutoComplete> stream = autoCompleteService.retrieveAllSalaries();
DataStreamUtility.streamObjects(stream, res, em);



}


}

Service:

@Service
public class AutoCompleteService {


private AutoCompleteRepository autocompleteRepository;

public AutoCompleteService(AutoCompleteRepository autRepo) {
this.autocompleteRepository = autRepo;
}

public List<AutoComplete> retrieveSalary(String jobClassCd) {
List<AutoComplete> salaries = autocompleteRepository.findByJobClassCd(jobClassCd);


if (salaries.size() <= 0) {
throw new SalaryNotFoundException(jobClassCd);
}

return salaries;
}


public Stream<AutoComplete> retrieveAllSalaries() {

return autocompleteRepository.streamAll();


}



}

最佳答案

处理请求的方法必须有一个带有 @RequestParam 注解的参数:

获取本地主机:8080/jobs?job_id=456

@GetMapping("/jobs", produces = "application/json")
public Book getJob(@RequestParam("job_id") Long id) {
return repo.findJobById(id);
}

因此,您的代码将如下所示:

@GetMapping("/jobs", produces = "application/json")
public List<AutoComplete> getSalary(
@RequestParam("autocomplete", required = true) String autocomplete,
@PathVariable("jobClassCd") String jobClassCd) {

return autoCompleteService.retrieveSalary(jobClassCd);

}

关于java - 如何通过java中的搜索正确请求与请求参数的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56569482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com