Table of Contents
Question content
Solution
Home Java springframework.web.client Whether RestClient handles @requestParam

springframework.web.client Whether RestClient handles @requestParam

Feb 10, 2024 pm 05:15 PM

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)