Java 从用户获取http请求,并使用RESTAPI将该可变值存储到任何数据结构中
我正在使用RESTAPI开发一个Web服务。我想获取用户在URL中键入的内容。例如,如果用户请求“”,那么我想获取“john”,以便使用该值进行进一步检查。我怎样才能得到那个变量Java 从用户获取http请求,并使用RESTAPI将该可变值存储到任何数据结构中,java,spring,rest,Java,Spring,Rest,我正在使用RESTAPI开发一个Web服务。我想获取用户在URL中键入的内容。例如,如果用户请求“”,那么我想获取“john”,以便使用该值进行进一步检查。我怎样才能得到那个变量 @Controller @RequestMapping("/employee") public class Employee { @RequestMapping(value="", method=RequestMethod.GET) public String disp(HttpServletRequest
@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");
}
}
如果只发送一个参数,您可以使用其他答案中提到的
@RequestParam
。但如果您有如此多的数据要从UI发布到控制器,那么请使用@RequestBody
注释并创建一个包含所有发送参数的模型类。然后发送的所有数据将自动绑定到模型。请参阅下面的代码
假设你的url是这样的
模范班
class UIMapper{
private String ename;
private String lastname;
private String address;
//Create getters and setters here
}
您的控制器类
class AController{
@RequestMapping(value = { "/webserviceInsert" })
public String webserviceInsert(@RequestBody UIMapper uiObj) {
String eName=uiObj.getEname();//Assume you have created getters and setters
return "success";
}
}
因此,你将在一个容易理解的对象中得到整个东西
从控制器传输到servcie或dao层
您必须指定正在使用的技术。是的,我正在使用Spring。您可以使用
@RequestParam
和@RequestVariable
,请尝试阅读本文,谢谢您的回复。我相信这应该是一个愚蠢的问题。但是你能告诉我在我的主要方法中应该如何阅读这个方法吗?
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class Employee {
@RequestMapping("/employee")
public String employeeDislpay(@RequestParam(value="ename") String ename) {
System.out.println(ename);
}
}