我正在使用rest API开发Web服务。我想获取用户在URL中键入的内容。例如,如果用户请求“ http://localhost:8080/employee?ename=john”,那么我想获取“ john”,以便可以使用该值进行进一步检查。如何获得该变量?

最佳答案

@Controller
@RequestMapping("/employee")
public class Employee {

  @RequestMapping(value="", method=RequestMethod.GET)
  public String disp(HttpServletRequest request, @RequestParam(value="ename", required=false) String ename) {
    // used
    System.out.println(ename); // voted
    // or
    request.getParameter("ename");
  }
}


https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

10-05 21:35