Springbootインターフェースには、共通の型パラメーターや@ReuqestBodyで変更されたパラメーターなど、複数の要求パラメーターがあります。Postmanを使用して呼び出す方法

1.背景紹介

他の人が開発したポストインターフェースを使用して、インターフェースに複数のリクエストパラメーターがあり、パラメーターの1つが@ReuqestBodyアノテーションで変更されていることがわかりました。この種のリクエストパラメータの場合、postmanを使用するときにリクエストする方法がわかりません。

2.さまざまなPostmanインターフェースのリクエストメソッドの概要

2.1フォームデータの受信

2.1.1パラメータは@RequestParamアノテーションで変更されます

インターフェースコード:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam("name") String name,
                        @RequestParam("age") Integer age) {
    
    
        return "name:" + name + "\nage:" + age;
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.1.2パラメータなし

インターフェイスにはパラメータがありますが、パラメータが渡されない場合、コントローラはエラーを報告します。これには2つの解決策があります。

  • パラメータが不要であることを示すには、required = falseを使用します
  • defaultValueを使用して、パラメーターのデフォルト値を指定します

インターフェースコード:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam(name = "name", defaultValue = "xxx") String name,
                        @RequestParam(name = "age", required = false) Integer age) {
    
    
        return "name:" + name + "\nage:" + age;
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.1.3リクエストパラメータはマップタイプです

インターフェースコード:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam Map<String,Object> params) {
    
    
        return "name:" + params.get("name") + "\nage:" + params.get("age");
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.1.4リクエストパラメータは配列です

フォームに同じ名前のパラメーターが複数ある場合、コントローラーはインターフェースコードを受け取る配列を定義できます

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestParam("name") String[] names) {
    
    
        String result = "";
        for(String name:names){
    
    
            result += name + "\n";
        }
        return result;
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.1.5パラメータはオブジェクトタイプです

POSTリクエストに含まれるパラメーターが多すぎる場合は、パラメーター受信インターフェイスコードを簡略化するためにオブジェクトを作成します

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(User user) {
    
    
        return "name:" + user.getName() + "\nage:" + user.getAge();
    }
}

Postmanリクエスト:
ここに画像の説明を挿入します
注1:渡されたパラメーターにプレフィックスがあり、そのプレフィックスがインターフェース内のパラメーターのエンティティークラス名と同じである場合、パラメーターも通常どおりに渡すことができます。
ここに画像の説明を挿入します
渡されたパラメーターにプレフィックスがある場合、プレフィックスが受信エンティティクラスの名前と異なる
ここに画像の説明を挿入します
場合、パラメータを正常に渡すことはできません。注2:getリクエストのパラメータが異なるオブジェクトに属している場合は、複数のオブジェクトを使用してパラメータインターフェイスコードを受信することもでき
ます。 :

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(User user, Phone phone) {
    
    
        return "name:" + user.getName() + "\nage:" + user.getAge()
                + "\nnumber:" + phone.getNumber();
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.2文字列テキストデータの受信

2.2.1パラメータはテキストタイプを作成します

テキストが渡されると、HttpServletRequestを介して入力ストリームを取得し、テキストコンテンツを読み取ることができます。

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(HttpServletRequest request) {
    
    
        ServletInputStream is = null;
        try {
    
    
            is = request.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = is.read(buf)) != -1) {
    
    
                sb.append(new String(buf, 0, len));
            }
            System.out.println(sb.toString());
            return "获取到的文本内容为:" + sb.toString();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                if (is != null) {
    
    
                    is.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
        return null;
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.3JSONデータの受信

2.3.1リクエストパラメータはマップタイプです

パラメーターがJSON形式で渡される場合、@ Requestbodyを使用してパラメーターを受け取り、データをMapインターフェースコードに変換
できます。

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestBody Map params) {
    
    
        return "name:" + params.get("name") + "\n age:" + params.get("age");
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.3.2オブジェクトタイプパラメータの受信

インターフェースコード:

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestBody User user){
    
    
        return user.getName() + " " + user.getAge();
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

2.3.3配列型パラメーターの受信

渡されたJSONデータが配列の場合、次のようにインターフェースコードを変更できます

package com.example.demo;
 
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
 
@RestController
public class HelloController {
    
    
    @PostMapping("/hello")
    public String hello(@RequestBody List<User> users){
    
    
        String result = "";
        for(User user:users){
    
    
            result += user.getName() + " " + user.getAge() + "\n";
        }
        return result;
    }
}

郵便配達員のリクエスト:
ここに画像の説明を挿入します

三、練習

調査の第2部の後、私が呼び出すように要求したサードパーティのインターフェイスについていくつかのアイデアがあります。私がリクエストしたいインタフェースが@RequestParam注釈で飾られた文字列型パラメータ持ち任意の注釈の装飾のないint型のパラメータ; @RequestBodyアノテーションで装飾され、リストのパラメータを

postmanリクエストを使用する場合、2つの方法があります。

方法1:
次のようなgetリクエストメソッドに従って最初の2つのパラメーターをURLに結合します:http:// localhost:8080 / api / test?param = sun&param2 = 5、3番目のリクエストはpostmanのjsonメソッド、つまりCanを使用しますインターフェイスの呼び出しを完了します

方法2:
最初の2つのパラメーターを郵便配達員の形式で入力しても、3番目のパラメーターはjsonモードでも要求されます

おすすめ

転載: blog.csdn.net/sinat_34241861/article/details/114384117