網頁

2021/4/15

Spring MVC @RequestParam 取得url參數

Spring MVC在Controller @RequestMapping method可用@RequestParam接收url參數。

例如下面DemoController.api1(@RequestParam String name, @RequestParam Integer age)接收/api?name={name}&age={age}的url請求。?後面的nameage的值分別對映到api1()@RequestParam String name@RequestParam Integer age@RequestParam設定的參數預設為必填,否則會回應404 Not Found。

除了使用個別的@RequestParam來接收不同的參數,也可直接用@RequestParam Map<String, String>來接收多個不同名稱的參數。例如DemoController.api2(@RequestParam Map<String, String> args)

而單個參數值可用逗號分隔並以List<String>參數接收。例如DemoController.api3(@RequestParam List<String> names)

DemoController

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class DemoController {

    @GetMapping("/api1")
    public String api1(@RequestParam String name, @RequestParam Integer age) {
        String result = String.format("name=%s,age=%s", name, age);
        System.out.println(result); // name=john,age=33
        return result;
    }

    @GetMapping("/api2")
    public String api2(@RequestParam Map<String, String> args) {
        String result = String.format("name=%s,age=%s", args.get("name"), args.get("age"));
        System.out.println(result); // name=john,age=33
        return result;
    }

    @GetMapping("/api3")
    public String api3(@RequestParam List<String> names) {
        String result = String.format("names=%s", String.join(",", names));
        System.out.println(result); // names=john,mary,bill
        return result;
    }
}

測試如下。

DemoControllerTests

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@AutoConfigureMockMvc
@SpringBootTest
public class DemoControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void api1_ok() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/api1?name=john&age=33"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("name=john,age=33"));
    }

    @Test
    public void api1_notFound() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/api1"))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }

    @Test
    public void api1_badRequest() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/api1?name=john&age=hello")) // age value is not int
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }

    @Test
    public void api2_ok() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/api2?name=john&age=33"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("name=john,age=33"));
    }

    @Test
    public void api3_ok() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/api3?names=john,mary,bill"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("names=john,mary,bill"));
    }
}


沒有留言:

張貼留言