Controller层常用注解:
来源:互联网 发布:淘宝助理 电子面单 编辑:程序博客网 时间:2024/06/16 11:28
Controller层常用注解:
@(我的第一个笔记本)[注解]
- @RestController 相当于“@Controller,@Controller 和@ResponseBody”的作用,在类开头上注明,作用是返回类中所有@RequestMapping(“”) 方法 的结果。
- @Autowired 和@Resource 都是注入bean,区别有待研究
- @GetMapping(“/{id}”) 的作用是获得Get请求,相当于@RequestMapping(method = RequestMethod.GET),可用于查询记录。
- @PostMapping(“/add”) 的作用是获得POST请求,相当@RequestMapping(method = RequestMethod.POST)., 可用于往数据表中添加记录。
- Restful 风格
- @RequestMapping(params=”tohello”) ,此处”tohello”是参数,在浏览器中访问时要在”?”后面添加参数名。如:http://localhost:8080/project/hello?tohello
//单个URL@GetMapping(value = {"/list/{deviceTypeId}","/list"})public BaseBody getAll(@PathVariable(value = "deviceTypeId",required = false) Long deviceTypeId) throws Exception {}//多个URL之间用逗号隔开,表示URL可选择参数@GetMapping(value = {"/{deviceTypeId}/{dataUnitId}","/{deviceTypeId}"}) public BaseBody getByDeviceTypeIdAndDataUnitId(@PathVariable("deviceTypeId") Long deviceTypeId, @PathVariable(value = "dataUnitId",required = false) Long dataUnitId){ }// **此代表deviceTypeID是与前台绑定的,可以有多个绑定数据,之间用逗号隔开, “required = false” 代表绑定的值可以为空,默认是true。 **
@RestController @RequestMapping("/data-unit")public class DataUnitController { @Autowired DataUnitService dataUnitService; @PostMapping("/add") public BaseBody add(@RequestBody DataUnitAddRB dataUnitAddRB) throws Exception { BaseBody baseBody = new BaseBody(); Long dataUnitId = dataUnitService.add(dataUnitAddRB.getDataType(),dataUnitAddRB.getDescription(), dataUnitAddRB.getOperation()); return baseBody.successResponse("创建成功",dataUnitId); } @GetMapping("/{id}") public BaseBody getById(@PathVariable("id") Long id) throws Exception { DataUnitDO dataUnitDO = dataUnitService.findById(id); BaseBody responseBody = new BaseBody(); return responseBody.successResponse("查询成功",dataUnitDO); } @GetMapping(value = {"/list/{deviceTypeId}","/list"}) public BaseBody getAll(@PathVariable(value = "deviceTypeId",required = false) Long deviceTypeId) throws Exception { List<DataUnitDO> dataUnitDOList; if(deviceTypeId == null){ dataUnitDOList = dataUnitService.findAll(); }else{ dataUnitDOList = dataUnitService.findByDeviceId(deviceTypeId); } BaseBody responseBody = new BaseBody(); return responseBody.successResponse("查询成功",dataUnitDOList); }/** * 设备心跳接口 * @param key */ @RequestMapping("/heart/{key}") public void keepAlive(@PathVariable("key") String key){ deviceService.heartBeatReceived(key); }//序号6的例子:浏览器访问:http://localhost:8080/jeewx/hello.do?toHello @RequestMapping(params = "toHello") public ModelAndView toHello(HttpServletRequest request, HttpServletResponse response, ModelMap model) { String openid = (String) request.getSession().getAttribute("openid"); System.out.println("openid 的值是:"+ openid); return new ModelAndView("wechat/hello"); }}
阅读全文
0 0
- Controller层常用注解:
- controller介绍及常用注解
- springMVC 注解 controller层的优化
- springmvc常用注解之@Controller和@RequestMapping
- springmvc常用注解之@Controller和@RequestMapping
- springmvc常用注解之@Controller和@RequestMapping
- springmvc常用注解之@Controller和@RequestMapping
- Spring常用注解@Component、@Controller、@ Service、@ Repository......
- SpringMVC常用注解@Controller,@Service,@repository,@Component
- springmvc常用注解之@Controller和@RequestMapping
- SpringMVC Controller介绍及常用注解
- SpringMVC Controller介绍及常用注解
- SpringMVC Controller介绍及常用注解
- SpringMVC Controller介绍及常用注解
- SpringMVC Controller介绍及常用注解
- SpringMVC Controller介绍及常用注解
- springmvc常用注解之@Controller和@ResquestMapping
- SpringMVC Controller介绍及常用注解
- Shrio
- [最短路 && 主席树维护HASH]Codeforces 464E. The Classic Problem
- leetcode202. Happy Number
- 使用jquery版本的viewer.js图片更新的问题
- 怎么设计产品?
- Controller层常用注解:
- java中的面向对象(封装、继承和多态)
- Unity学习笔记002-Hellow World
- C++ primer 学习笔记
- linux centos 笔记
- Python笔记(十三)--时间和日期
- 1~n 整数中 1 出现的次数
- thinkPHP 微信生成带参数的二维码
- 手机移动端WEB资源