iis5建设网站,包头哪里做网站,芜湖北京网站建设,电子商务网站建设中应注意哪些问题在SpringBoot应用开发中#xff0c;处理Http请求是一项基础且重要的任务。Spring Boot通过提供一系列丰富的注解极大地简化了这一过程#xff0c;使得定义请求处理器和路由变得更加直观与便捷。这些注解不仅帮助开发者清晰地定义不同类型的HTTP请求如何被处理#xff0c;同时…在SpringBoot应用开发中处理Http请求是一项基础且重要的任务。Spring Boot通过提供一系列丰富的注解极大地简化了这一过程使得定义请求处理器和路由变得更加直观与便捷。这些注解不仅帮助开发者清晰地定义不同类型的HTTP请求如何被处理同时也提升了代码的可读性和维护性。
一、RequestMapping
RequestMapping用于将特定的HTTP请求映射到特定的方法上。可用于类级别和方法级别上以下是代码示例。
RestController
RequestMapping(/customer-api/) // 类级别所有方法都以customer-api开头
public class CustomerApi{RequestMapping(value /get-customer-by-id, method RequestMethod.GET){ // 方法级别当收到对/customer-api/get-customer-by-id路径的GET请求时会调用getCustomerById方法。ResponseCustomer getCustomerById(RequestParam(id) Integer id)……}
}本节示例中RestController和RequestParam注解可以先忽略下面会介绍到。
二、PutMapping、DeleteMapping、GetMapping和PostMapping
为了更加明确表示不同的HTTP方法Spring Boot提供了一组特定的注解分别对应PUT, DELETE、GET、POST增删改查请求。
RestController
RequestMapping(/customer-api/)
public class CustomerApi{PutMapping(/add-customer) // 增加的请求,和RequestMapping的PUT方式等价Response? addCustomer(RequestBody Customer customer)……}DeleteMapping(/delete-customer-by-id) // 删除的请求,和RequestMapping的DELETE方式等价Response? deleteCustomer(RequestParam(id) Integer id)……}PostMapping(/update-customer) // 更新的请求,和RequestMapping的POST方式等价Response? updateCustomer(RequestBody Customer customer)……}// RequestMapping(value /get-customer-by-id, method RequestMethod.GET) // 方式1GetMapping(/get-customer-by-id) // 和方式1是等价的。ResponseCustomer getCustomerById(RequestParam(id) Integer id)……}
}三、RequestParam和PathVariable
RequestParam用于获取查询参数PathVariable用于获取路径变量
GetMapping(/get-customer-by-id){ ResponseCustomer getCustomerById(RequestParam(id) Integer id) // RequestParam就可获取请求路径/get-customer-by-id?id1中的id的值……}GetMapping(/get-customer/{id}){ ResponseCustomer getCustomerById(PathVariable(id) Integer id) // PathVariable可获取路径/get-customer/{id}中的id的值……}四、RequestBody
RequestBody注解用于将 HTTP 请求的主体内容绑定到方法的参数上。通常用于处理 POST 和 PUT 请求当请求的主体是 JSON 或 XML 格式的数据时非常有用。 示例见第二节这里就不重复赘述了。
五、RestController和Controller
RestController和Controller都是用于定义控制器类的注解但是两者之间有细微的差异。 RestController是一个组合注解相当于Controller和ResponseBody。 用RestController标注的API类其中的方法会直接返回数据如JSON、XML,不会返回视图。 Controller刚好相反它标注的类中的方法会直接返回视图如JSP、Thymeleaf模版等。
小结现在的微服务项目基本都是前后端分离所以Controller已经慢慢的淡出了视野很少使用而RestController已然成为了主流。
后面有时间了在聊聊GET和POST请求的区别大厂面试被问到的频次贼高。 文章转载自: http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.xsszn.cn.gov.cn.xsszn.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.fpyll.cn.gov.cn.fpyll.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.nllst.cn.gov.cn.nllst.cn http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn http://www.morning.yfnhg.cn.gov.cn.yfnhg.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.ryxdr.cn.gov.cn.ryxdr.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.ywqsk.cn.gov.cn.ywqsk.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.bnrff.cn.gov.cn.bnrff.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.qsdnt.cn.gov.cn.qsdnt.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.kpgms.cn.gov.cn.kpgms.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.jhrkm.cn.gov.cn.jhrkm.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.xrksf.cn.gov.cn.xrksf.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.wrlff.cn.gov.cn.wrlff.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn