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

springframework.web.client Whether RestClient handles @requestParam

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2024-02-10 17:15:07
forward
1010 people have browsed it

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 ....;
    }
Copy after login
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>>() {});
Copy after login
<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>
Copy after login

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())
    // ...
Copy after login

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(...);
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template