Spring MVC 参数传递方式后端代码汇总

参数传递方式大类分为两种:

  1.  Form 格式
  2. JSON 格式


Form格式:
@RequestMapping(value="/{username}", method=GET)
public String showSpitterProfile(
@PathVariable String username, Model model) {
Spitter spitter = spitterRepository.findByUsername(username);
model.addAttribute(spitter);
return "profile";
}
// Get a Single Note
@GetMapping("/notes/{id}")
public Note getNoteById(@PathVariable(value = "id") Long noteId) {
    return noteRepository.findById(noteId)
            .orElseThrow(() -> new ResourceNotFoundException("Note", "id", noteId));
}
@RequestMapping(method=RequestMethod.GET)
public List<Spittle> spittles(
@RequestParam("max") long max,
@RequestParam("count") int count) {
return spittleRepository.findSpittles(max, count);
}
@RequestMapping(value="/show", method=RequestMethod.GET)
public String showSpittle(
@RequestParam("spittle_id") long spittleId,
Model model) {
model.addAttribute(spittleRepository.findOne(spittleId));
return "spittle";
}

JSON格式:
@RequestMapping(value = "/api/queryCompanies", method = RequestMethod.POST)
@ResponseBody
public List<Company> queryCompanies(@RequestBody ReqParams reqParam) {
     
  List<Company> companies = (List<Company>) companyService.findByNameLike(reqParam.getCompanyName());
     
  return companies;
}

猜你喜欢

转载自blog.csdn.net/java_augur/article/details/80027318