spring boot get和post请求

本文详细介绍了Spring MVC框架中如何处理不同类型的HTTP请求,包括GET和POST请求,并演示了如何使用@RequestParam、@PathVariable和@RequestBody等注解进行参数绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GET、POST方式提时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
  •     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
  •     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
代码:

[java]  view plain  copy
  1. package com.example.controller;  
  2.   
  3. import org.springframework.web.bind.annotation.GetMapping;  
  4. import org.springframework.web.bind.annotation.PathVariable;  
  5. import org.springframework.web.bind.annotation.RequestBody;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestMethod;  
  8. import org.springframework.web.bind.annotation.RequestParam;  
  9. import org.springframework.web.bind.annotation.RestController;  
  10.   
  11. import com.example.bean.RequestLoginBean;  
  12. import com.example.response.BaseResponse;  
  13. import com.google.gson.Gson;  
  14.   
  15. @RestController  
  16. @RequestMapping(value = "/index")  
  17. public class Login {  
  18.   
  19.     /** 
  20.      * index home 
  21.      *  
  22.      * @return 
  23.      */  
  24.     @RequestMapping(value = "/home")  
  25.     public String home() {  
  26.         return "index home";  
  27.     }  
  28.       
  29.     /** 
  30.      * 得到1个参数 
  31.      *  
  32.      * @param name 
  33.      *            用户名 
  34.      * @return 返回结果 
  35.      */  
  36.     @GetMapping(value = "/{name}")  
  37.     public String index(@PathVariable String name) {  
  38.         return "oh you are " + name + "<br> nice to meet you";// \n不起作用了,那就直接用html中的标签吧  
  39.     }  
  40.   
  41.     /** 
  42.      * 简单post请求 
  43.      *  
  44.      * @param name 
  45.      * @param pwd 
  46.      * @return 
  47.      */  
  48.     @RequestMapping(value = "/testpost", method = RequestMethod.POST)  
  49.     public String testpost() {  
  50.         System.out.println("hello  test post");  
  51.         return "ok";  
  52.     }  
  53.   
  54.     /** 
  55.      * 同时得到两个参数 
  56.      *  
  57.      * @param name 
  58.      *            用户名 
  59.      * @param pwd 
  60.      *            密码 
  61.      * @return 返回结果 
  62.      */  
  63.     @GetMapping(value = "/login/{name}&{pwd}")  
  64.     public String login(@PathVariable String name, @PathVariable String pwd) {  
  65.         if (name.equals("admin") && pwd.equals("admin")) {  
  66.             return "hello welcome admin";  
  67.         } else {  
  68.             return "oh sorry user name or password is wrong";  
  69.         }  
  70.     }  
  71.   
  72.     /** 
  73.      * 通过get请求去登陆 
  74.      *  
  75.      * @param name 
  76.      * @param pwd 
  77.      * @return 
  78.      */  
  79.     @RequestMapping(value = "/loginbyget", method = RequestMethod.GET)  
  80.     public String loginByGet(@RequestParam(value = "name", required = true) String name,  
  81.             @RequestParam(value = "pwd", required = true) String pwd) {  
  82.         return login4Return(name, pwd);  
  83.     }  
  84.   
  85.     /** 
  86.      * 通过post请求去登陆 
  87.      *  
  88.      * @param name 
  89.      * @param pwd 
  90.      * @return 
  91.      */  
  92.     @RequestMapping(value = "/loginbypost", method = RequestMethod.POST)  
  93.     public String loginByPost(@RequestParam(value = "name", required = true) String name,  
  94.             @RequestParam(value = "pwd", required = true) String pwd) {  
  95.         System.out.println("hello post");  
  96.         return login4Return(name, pwd);  
  97.     }  
  98.   
  99.     /** 
  100.      * 参数为一个bean对象.spring会自动为我们关联映射 
  101.      * @param loginBean 
  102.      * @return 
  103.      */  
  104.     @RequestMapping(value = "/loginbypost1", method = { RequestMethod.POST, RequestMethod.GET })  
  105.     public String loginByPost1(RequestLoginBean loginBean) {  
  106.         if (null != loginBean) {  
  107.             return login4Return(loginBean.getName(), loginBean.getPwd());  
  108.         } else {  
  109.             return "error";  
  110.         }  
  111.     }  
  112.       
  113.     /** 
  114.      * 请求内容是一个json串,spring会自动把他和我们的参数bean对应起来,不过要加@RequestBody注解 
  115.      *  
  116.      * @param name 
  117.      * @param pwd 
  118.      * @return 
  119.      */  
  120.     @RequestMapping(value = "/loginbypost2", method = { RequestMethod.POST, RequestMethod.GET })  
  121.     public String loginByPost2(@RequestBody RequestLoginBean loginBean) {  
  122.         if (null != loginBean) {  
  123.             return login4Return(loginBean.getName(), loginBean.getPwd());  
  124.         } else {  
  125.             return "error";  
  126.         }  
  127.     }  
  128.   
  129.       
  130.   
  131.   
  132.     /** 
  133.      * 对登录做出响应处理的方法 
  134.      *  
  135.      * @param name 
  136.      *            用户名 
  137.      * @param pwd 
  138.      *            密码 
  139.      * @return 返回处理结果 
  140.      */  
  141.     private String login4Return(String name, String pwd) {  
  142.         String result;  
  143.         BaseResponse response = new BaseResponse();  
  144.         if (name.equals("admin") && pwd.equals("admin")) {  
  145.             result = "hello welcome admin";  
  146.             response.setState(true);  
  147.         } else {  
  148.             result = "oh sorry user name or password is wrong";  
  149.             response.setState(false);  
  150.         }  
  151.         System.out.println("收到请求,请求结果:" + result);  
  152.         return new Gson().toJson(response);  
  153.     }  
  154. }  
Spring Boot Gateway 是基于 Spring Framework 的响应式网关,它可以帮助开发者在微服务架构中提供统一的路由过滤器功能。在 Spring Boot Gateway 中,你可以通过编写路由规则来指定哪些请求被转发到特定的服务。如果你需要配置只转发 GET POST 请求,你可以使用内置的谓词工厂来实现这一需求。 下面是一个配置示例,展示了如何在 Spring Boot Gateway 中只转发 GET POST 请求: ```java import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("get_post_route", r -> r .method(HttpMethod.GET, HttpMethod.POST) .path("/your/path/**") .filters(f -> f.stripPrefix(1)) .uri("http://your-service-url")) .build(); } } ``` 在上面的代码中: - `RouteLocatorBuilder` 用于构建路由规则。 - `route` 方法定义了一个路由,其中 `"get_post_route"` 是路由的唯一标识。 - `method(HttpMethod.GET, HttpMethod.POST)` 指定了只有 GET POST 请求才会被这个路由规则匹配。 - `path("/your/path/**")` 指定了请求路径的匹配模式,这里使用 `/**` 来匹配所有以 `/your/path` 开头的路径。 - `filters(f -> f.stripPrefix(1))` 是一个过滤器配置,这里使用 `stripPrefix` 来去掉路径中的一级前缀。 - `uri("http://your-service-url")` 指定了请求被转发到的目标服务地址。 通过这样的配置,只有 GET POST 请求会被路由到指定的后端服务,其他类型的请求则不会被转发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值