spring boot 常见http请求url参数获取

spring boot 常见http请求url参数获取

在定义一个Rest接口时通常会利用GET、POST、PUT、DELETE来实现数据的增删改查;这几种方式有的需要传递参数,后台开发人员必须对接收到的参数进行参数验证来确保程序的健壮性

  • GET:一般用于查询数据,采用明文进行传输,一般用来获取一些无关用户信息的数据

  • POST:一般用于插入数据

  • PUT:一般用于数据更新

  • DELETE:一般用于数据删除;一般都是进行逻辑删除(即:仅仅改变记录的状态,而并非真正的删除数据)

1、@PathVaribale 获取url中的数据

请求URL:localhost:8080/hello/id 获取id值

实现代码如下:

@RestController
publicclass HelloController {    
    
    @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)    
    public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){        
        return"id:"+id+" name:"+name;    
    }
    
}

在浏览器中 输入地址:

localhost:8080/hello/100/hello

输出:

id:81name:hello

2、@RequestParam 获取请求参数的值

获取url参数值,默认方式,需要方法参数名称和url参数保持一致

请求URL:localhost:8080/hello?id=1000

@RestController
publicclass HelloController {  
    
    @RequestMapping(value="/hello",method= RequestMethod.GET)    
    public String sayHello(@RequestParam Integer id){       
        return"id:"+id;    
    }
    
}

输出:id:100

url中有多个参数时,如:

localhost:8080/hello?id=98&&name=helloworld

具体代码如下:

@RestController
publicclass HelloController {    
    
    @RequestMapping(value="/hello",method= RequestMethod.GET)   
    public String sayHello(@RequestParam Integer id,@RequestParam String name){  
        return"id:"+id+ " name:"+name;  
    }
    
}

获取url参数值,执行参数名称方式

localhost:8080/hello?userId=1000

@RestController
publicclass HelloController {   
    
    @RequestMapping(value="/hello",method= RequestMethod.GET)   
    public String sayHello(@RequestParam("userId") Integer id){    
        return"id:"+id;  
    }
    
}

输出:id:100

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中进行高效的HTTP调用,并携带Cookie和请求头参数,您可以使用RestTemplate或WebClient。这两个类都是Spring提供的用于进行HTTP调用的工具。 1. 使用RestTemplate进行HTTP调用: 首先,确保在您的项目中包含以下Maven依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 然后,在您的代码中使用RestTemplate进行HTTP调用: ```java import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; public class HttpClientUtil { public static ResponseEntity<String> sendRequest(String url, HttpMethod method, MultiValueMap<String, String> headers, String cookie) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(headers); httpHeaders.add("Cookie", cookie); HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders); return restTemplate.exchange(url, method, httpEntity, String.class); } } ``` 在上面的示例中,`sendRequest` 方法接受URLHTTP方法、请求头参数和Cookie作为参数,并返回一个包含响应的 ResponseEntity 对象。 2. 使用WebClient进行HTTP调用: 首先,确保在您的项目中包含以下Maven依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> ``` 然后,在您的代码中使用WebClient进行HTTP调用: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; public class HttpClientUtil { public static Mono<String> sendRequest(String url, HttpMethod method, HttpHeaders headers, String cookie) { WebClient webClient = WebClient.builder() .baseUrl(url) .defaultHeaders(httpHeaders -> { httpHeaders.addAll(headers); httpHeaders.setContentType(MediaType.APPLICATION_JSON); httpHeaders.add("Cookie", cookie); }) .build(); return webClient.method(method) .retrieve() .bodyToMono(String.class); } } ``` 在上面的示例中,`sendRequest` 方法接受URLHTTP方法、请求头参数和Cookie作为参数,并返回一个 Mono 对象,其中包含响应结果。 使用上述任一方法,您可以在Spring Boot应用程序中进行高效的HTTP调用,并携带Cookie和请求头参数。根据您的需求,您可以扩展这些示例以满足更复杂的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值