当前位置: 代码迷 >> 综合 >> RestFul spring 的GET和POST接口编写
  详细解决方案

RestFul spring 的GET和POST接口编写

热度:4   发布时间:2024-01-04 15:03:36.0

Get

@ResponseBody//表示有返回值
@RequestMapping(value="/empst",method=RequestMethod.GET,produces="text/html;charset=UTF-8")public String getInterfaceDataByBusiAndType(@RequestParam(value="userId")String userId)throws Exception{//方法体
}
@GET@Path("getuser1/{name},{pwd}")@Produces(MediaType.APPLICATION_JSON)public String getUser1(@PathParam("name")String name,@PathParam("pwd")String pwd){
//		List<Users> user = new ArrayList<Users>();Session session = HibernateSessionFactory.getSession();String shql = "from Users where uname = ? ";//Query q = session.createQuery(shql);q.setString(0, name);Users user = (Users)q.uniqueResult();boolean isSucc = false;if(user != null){if(user.getUpwd().equals(pwd)){isSucc = true;}}HibernateSessionFactory.closeSession();if(isSucc){return "{\"login\":[{\"state\":\"1\"}]}";}else{return "{\"login\":[{\"state\":\"0\"}]}";}}

get请求是可以在url中通过? & 拼接参数,主要用于查询数据库信息

POST

@RequestMapping(value="/audit",method=RequestMethod.POST,produces="application/json;charset=UTF-8")public @ResponseBody String auditData(@RequestBody String val){
}

在请求时,需要将请求的参数放到StringEntity进行传参数传递,post请求详情可以查看

HTTP 调用外部post接口

 

  相关解决方案