BAEL-6080: Getting Query String Parameters from HttpServletRequest

This commit is contained in:
Forb Yuan 2023-12-26 12:03:02 +08:00
parent 9dd807367b
commit cd654c9e20
No known key found for this signature in database
GPG Key ID: A926E0F7CAE13008
3 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package com.baeldung.requestparam;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RestController
public class QueryStringController {
@GetMapping("/api/user0")
public String byGetQueryString(HttpServletRequest request) {
return request.getQueryString();
}
@GetMapping("/api/user1")
public String byGetParameter(HttpServletRequest request) {
String username = request.getParameter("username");
return "username:" + username;
}
@GetMapping("/api/user2")
public String byGetParameterValues(HttpServletRequest request) {
String[] roles = request.getParameterValues("roles");
return "roles:" + Arrays.toString(roles);
}
@GetMapping("/api/user3")
public UserDTO byGetParameterMap(HttpServletRequest request) {
Map<String, String[]> parameterMap = request.getParameterMap();
String[] usernames = parameterMap.get("username");
String[] roles = parameterMap.get("roles");
UserDTO userDTO = new UserDTO();
userDTO.setUsername(usernames[0]);
userDTO.setRoles(Arrays.asList(roles));
return userDTO;
}
@GetMapping("/api/user4")
public UserDTO byParameterName(String username, String[] roles) {
UserDTO userDTO = new UserDTO();
userDTO.setUsername(username);
userDTO.setRoles(Arrays.asList(roles));
return userDTO;
}
@GetMapping("/api/user5")
public UserDTO byRequestParamAnnotation(@RequestParam("username") String var1, @RequestParam("roles") List<String> var2) {
UserDTO userDTO = new UserDTO();
userDTO.setUsername(var1);
userDTO.setRoles(var2);
return userDTO;
}
@GetMapping("/api/user6")
public UserDTO byPojo(UserDTO userDTO) {
return userDTO;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.requestparam;
import java.util.List;
public class UserDTO {
private String username;
private List<String> roles;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.requestparam;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
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 static org.hamcrest.Matchers.containsInRelativeOrder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
@SpringBootTest
@AutoConfigureMockMvc
class QueryStringControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@BeforeEach
public void setup() {
this.mockMvc = standaloneSetup(new QueryStringController()).build();
}
@Test
public void whenInvokeGetQueryString_thenReturnTheOriginQueryString() throws Exception {
this.mockMvc.perform(get("/api/user0?username=bob&roles=admin&roles=stuff"))
.andExpect(status().isOk())
.andExpect(content().string("username=bob&roles=admin&roles=stuff"));
}
@Test
public void whenInvokeGetQueryParameter_thenReturnOneParameterValue() throws Exception {
this.mockMvc.perform(get("/api/user1?username=bob"))
.andExpect(status().isOk())
.andExpect(content().string("username:bob"));
}
@Test
public void whenInvokeGetParameterValues_thenReturnParameterAsArray() throws Exception {
this.mockMvc.perform(get("/api/user2?roles=admin&roles=stuff"))
.andExpect(status().isOk())
.andExpect(content().string("roles:[admin, stuff]"));
}
@ParameterizedTest
@CsvSource(textBlock = """
/api/user3
/api/user4
/api/user5
/api/user6
""")
public void whenPassParameters_thenReturnResolvedModel(String path) throws Exception {
this.mockMvc.perform(get(path + "?username=bob&roles=admin&roles=stuff"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.username").value("bob"))
.andExpect(jsonPath("$.roles").value(containsInRelativeOrder("admin", "stuff")));
}
}