@RequestMappering和Restful

1.@RequestMappering

@RequestMapping是一个用来处理请求地址到处理器controller功能方法映射规则的注解,这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法controller上,可用于类或方法上。注解在类上,表示类中的所有响应请求的方法都是以该地址作为父路径(模块路径)。

意思是说这个注解可以处理客户端请求的地址,将地址映射到类上或者方法上面,

1.url路径映射:

@RequestMapping("/xxx"@RequestMapping(value = "/xxx"@RequestMapping(path  = "/xxx"@RequestMapping(value  = {"/xxx","/yyy"}//可以将多个url映射到同于1个类上或方法上
                
@RequestMapping(path  = {"/xxx","/yyy"}//可以将多个url映射到同于1个类上或方法上    
 
//method属性是指定请求类型,GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等                
@RequestMapping (path = "/xxx",method = RequestMethod.GET)    
                

1.只注解在方法上面:

访问路径:http://localhost:8080 / 项目名+RequestMapping注解参数

@Controller//代表这个类会被spring接管  被这个注解的类中的所有方法如果值是String 并且有具体的页面可以跳转,就会被视图器解析

	public class ControllerTest1{
    @RequestMapping("/t1")//请求地址 等价于@RequestMapping(value="/t1")、@RequestMapping(path="/t1")
    public String test1(Model model){
        model.addAttribute("msg","ControllerTest1test1");
        return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.sp
    }
    //访问地址:http://localhost:8080/t1
    @RequestMapping("/t2")//请求地址
    public String test2(Model model){
        model.addAttribute("msg","ControllerTest1test2");
        return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.jsp
    }
    //访问地址:http://localhost:8080/t2
    }

http://localhost:8080/t1
在这里插入图片描述
http://localhost:8080/t2
在这里插入图片描述
2.同时注解在类名和方法名上面

访问路径:http://localhost:8080 / 项目名+类注解参数 +方法注解参数 , 需要先指定类的路径再指定方法的路径;

@RequestMapping("t/")
public class ControllerTest2 {
    @RequestMapping("/t1")//请求地址
    public String test1(Model model){
        model.addAttribute("msg","ControllerTest2test1");
        return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.jsp
    }
    //访问地址:http://localhost:8080/t/t1
    
    @RequestMapping("/t2")//请求地址
    public String test2(Model model){
        model.addAttribute("msg","ControllerTest2test2");
        return "test";//返回值会被交给视图解析器处理并拼接为WEB-INF/jsp/test.jsp
    }
    //访问地址:http://localhost:8080/t/t2
}

http://localhost:8080/t/t1
在这里插入图片描述
http://localhost:8080/t/t2
在这里插入图片描述

3.多个url映射到同于1个类上或方法上

//其中的path可换成value
@Controller
@RequestMapping(path={"/c","/d"})
public class ControllerTest3 {
    @RequestMapping(path={"/c3/t1","/c3/t11"})
    public String test1(Model model){
        model.addAttribute("msg","ControllerTest3");
        return "test";
    }
}
//访问地址:http://localhost:8080/c/c3/t1、http://localhost:8080/c/c3/t11
//http://localhost:8080/d/c3/t1、http://localhost:8080/d/c3/t11

在这里插入图片描述
在这里插入图片描述

2.RestFul风格

概念

Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

功能

资源:互联网所有的事物都可以被抽象为资源

资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。

分别对应 添加、 删除、修改、查询。

传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get

​ http://127.0.0.1/item/queryItem.action?id=1 查询,GET

​ http://127.0.0.1/item/saveItem.action 新增,POST

​ http://127.0.0.1/item/updateItem.action 更新,POST

​ http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST

使用RESTful操作资源 :可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!

​ http://127.0.0.1/item/1 查询,GET

​ http://127.0.0.1/item 新增,POST

​ http://127.0.0.1/item 更新,PUT

​ http://127.0.0.1/item/1 删除,DELETE

1.新建一个类Controller

@Controller
public class RestfulController {

}

2.使用 @PathVariable将 URL 中占位符参数绑定到控制器处理方法的入参中

//Restful:http://localhost:8080/add/a/b
//映射访问路径,通过GET方式提交的请求:
@RequestMapping (path = "add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a, @PathVariable String b, Model model){
    String res=a+b;
    model.addAttribute("msg","结果1:"+res);
    return "test";
}
}

测试:

访问地址:http://localhost:8080/add/1/2
在这里插入图片描述
浏览器默认使用Get方式提交请求,所以能显示结果
在这里插入图片描述

将method改为post

//映射访问路径,POST方式提交
@RequestMapping (path = "add/{a}/{b}",method = RequestMethod.POST)
public String test1(@PathVariable int a, @PathVariable String b, Model model){
    String res=a+b;
    model.addAttribute("msg","结果1:"+res);
    return "test";
}

设置请求方式设置为Get 所以post请求不被允许执行方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2E42p9kQ-1652776916038)(C:\Users\hry508816739\AppData\Roaming\Typora\typora-user-images\image-20220517125946932.png)]

@RequestMapping(method = RequestMethod.XXX) 

等价的组合注解:

@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
//映射访问路径,Get方式提交
@RequestMapping (path = "add/{a}/{b}",method = RequestMethod.GET)
public String test1(@PathVariable int a, @PathVariable String b, Model model){
    String res=a+b;
    model.addAttribute("msg","结果1:"+res);
    return "test";
}

//或
//@GetMapping ("add/{a}/{b}")
//当映射路径中的参数与控制器方法中的参数名不同时
//public String test2(@PathVariable("a") int a1, @PathVariable("b") String b1, Model model){
//    String res=a1+b1;
//    model.addAttribute("msg","结果2:"+res);
//    return "test";
//}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Feign是一个声明式的Web服务客户端,它可以通过注解方式来实现对RESTful风格的服务的调用。它可以将我们需要调用的服务抽象成接口,并使用注解来定义接口的请求方式、路径、参数等信息。 RESTful是一种设计风格,它是一种基于HTTP协议的统一接口风格,用于构建可伸缩性强、可维护性高的Web服务。RESTful风格的服务通过URL来标识资源,通过HTTP动词(GET、POST、PUT、DELETE等)来操作资源。 在使用Feign时,我们可以通过注解方式定义接口的请求方式,以及URL路径和参数的映射关系,使得我们可以通过接口的方式调用RESTful风格的服务。 例如,我们可以使用`@FeignClient`注解来声明一个Feign客户端,其中的`name`属性表示要调用的服务的名称。然后我们可以在接口中使用`@RequestMapping`注解来定义请求的URL路径和请求方式,使用`@RequestParam`来定义请求参数。通过这种方式,我们可以实现对RESTful风格服务的调用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [使用Feign实现声明式Restful风格调用](https://blog.csdn.net/fanrenxiang/article/details/78499935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java使用Feign实现声明式Restful风格调用](https://download.csdn.net/download/weixin_38583278/12749610)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原首

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值