当前位置: 移动技术网 > IT编程>开发语言>c# > 深入浅析Restful接口的两种使用方式

深入浅析Restful接口的两种使用方式

2019年07月18日  | 移动技术网IT编程  | 我要评论
为什么用restful接口? 怎么用呢? restful接口常用的两种方式是get和post.下面简单介绍一下这两种方式的使用. 由于调用restfu

为什么用restful接口?

怎么用呢?

restful接口常用的两种方式是get和post.下面简单介绍一下这两种方式的使用.

由于调用restful接口是通过url的方式来访问后端的代码.新建custregisterapi类以后,除了基本的注入外,还需要配置url的地址.以后的demo就在这个类里面写了.

<strong><span style="font-size:18px;">@restcontroller
@requestmapping(value = "/customer/register", produces = { mediatype.application_json_utf8_value })
@crossorigin(origins = "*")
public class custregisterapi {
 @autowired
 private httpservletrequest request;
 @autowired
 private httpservletresponse response; 
}</span></strong>

1:get方式,url地址会在地址栏显示出参数.

<strong><span style="font-size:18px;">/**
 * 检查邮箱是否已经绑定
 * @param email 邮箱
 * @return
 */
 @requestmapping(value = "/checkemail", method = { requestmethod.get })
 @apioperation(value = "检查邮箱是否已经绑定")
 public restresponse<boolean> checkemail(@requestparam(value = "email") string email) {
 restresponse<boolean> restresponse = null;
 try {
 boolean checkismailbinding = custservice.checkismailbinding(email);
 // restresponse = new restresponse<boolean>(restrespcode.ok, messageutil.getmessage(restrespcode.ok),
 // checkismailbinding);
 if (checkismailbinding == false) {
 restresponse = new restresponse<boolean>(restrespcode.register_username_existed,
  messageutil.getmessage(restrespcode.register_username_existed), null);
 } else {
 restresponse = new restresponse<boolean>(restrespcode.ok, messageutil.getmessage(restrespcode.ok), null);
 }
 } catch (exception e) {
 e.printstacktrace();
 restresponse = new restresponse<boolean>(restrespcode.internal_error,
  messageutil.getmessage(restrespcode.internal_error), null);
 }
 return restresponse;
 }</span></strong>

访问方式:http://localhost:8080(端口号)/模块名称/register/checkemail?email=****

:post方式,url地址会在地址栏不会显示出参数.

<strong><span style="font-size:18px;">/**
 * 修改密码
 * @param memberid 用户编号
 * @param oldpassword 旧密码
 * @param newpassword 新密码
 * @return
 * @throws exception
 */
 @requestmapping(value = "/modifypassword", method = requestmethod.post, consumes = "application/json")
 @apioperation(value = "修改支付密码")
 public restresponse<boolean> changepassword(@requestbody captchavo captchavo) throws exception {
 // 验证旧密码是否正确
 boolean findpassword = registerservice.findpassword(captchavo.getmemberid(), captchavo.getoldpassword());
 if (findpassword == false) {
 return new restresponse<boolean>(restrespcode.password_wrong,
  messageutil.getmessage(restrespcode.password_wrong), null);
 }
 return new restresponse<boolean>(restrespcode.ok, messageutil.getmessage(restrespcode.ok), null);
 }</span></strong>

post方式

是通过application/json;charset=utf-8来访问一级custom的方式来访问,一般是用于修改密码或者是不让别人看到参数的情况下用的post方式.

在测试的时候我是用火狐浏览器上的resteasy插件来进行测试的.

总结

以上所述是小编给大家介绍的restful接口的两种使用方式,希望对大家有所帮助!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网