Home  >  Article  >  springframework.web.client Whether RestClient handles @requestParam

springframework.web.client Whether RestClient handles @requestParam

WBOY
WBOYforward
2024-02-10 17:15:07841browse

php Editor Banana will answer a question about Spring Framework: Does RestClient handle @RequestParam? In Spring Framework, when using RestTemplate to make an HTTP request, the @RequestParam annotation is used to obtain parameters from the request URL. So can RestClient correctly handle the @RequestParam annotation? Next, we will explore this issue in detail to help you better understand and use RestClient in Spring Framework.

Question content

I tried this:

@getmapping("/db/video/getall")
public responseentity<list<videodto>> getallvideo(@requestparam string owneremail) {        
        return ....;
    }
import org.springframework.web.client.restclient;

restclient restclient = restclient.create("http://localhost:8095");

list<videodto> result = restclient.get()
    .uri("/db/video/getall")
    .accept(mediatype.application_json)
    .retrieve()
    .body(new parameterizedtypereference<list<videodto>>() {});
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>3.2.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>6.1.2</version>
  </dependency>

But I can't find a code example showing how restclient passes owneremail as @requestparam.

I found the @pathvariable code example.

So, does restclient handle @requestparam?

Solution

For get requests, you can add query parameters in the url. Spring's uricomponentsbuilder simplifies the operation of uri.

restclient.get()
    .uri(uricomponentsbuilder.frompath("/db/video/getall")
            .queryparam("owneremail", "email").touristring())
    // ...

For post requests, the body can be set to multivaluemap. The content-type can be inferred from its content.

var parts = new LinkedMultiValueMap<String, Object>();
parts.add("ownerEmail", "emailValue");
restClient.post().uri("/db/video/getall").body(parts).retrieve().body(...);

The above is the detailed content of springframework.web.client Whether RestClient handles @requestParam. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete