BAEL-6080: Getting Query String Parameters from HttpServletRequest
This commit is contained in:
parent
cd654c9e20
commit
69eddf18d3
@ -1,64 +1,65 @@
|
|||||||
package com.baeldung.requestparam;
|
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.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class QueryStringController {
|
public class QueryStringController {
|
||||||
|
|
||||||
@GetMapping("/api/user0")
|
@GetMapping("/api/byGetQueryString")
|
||||||
public String byGetQueryString(HttpServletRequest request) {
|
public String byGetQueryString(HttpServletRequest request) {
|
||||||
return request.getQueryString();
|
return request.getQueryString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/api/user1")
|
@GetMapping("/api/byGetParameter")
|
||||||
public String byGetParameter(HttpServletRequest request) {
|
public String byGetParameter(HttpServletRequest request) {
|
||||||
String username = request.getParameter("username");
|
String username = request.getParameter("username");
|
||||||
return "username:" + username;
|
return "username:" + username;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/api/user2")
|
@GetMapping("/api/byGetParameterValues")
|
||||||
public String byGetParameterValues(HttpServletRequest request) {
|
public String byGetParameterValues(HttpServletRequest request) {
|
||||||
String[] roles = request.getParameterValues("roles");
|
String[] roles = request.getParameterValues("roles");
|
||||||
return "roles:" + Arrays.toString(roles);
|
return "roles:" + Arrays.toString(roles);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/api/user3")
|
@GetMapping("/api/byGetParameterMap")
|
||||||
public UserDTO byGetParameterMap(HttpServletRequest request) {
|
public UserDto byGetParameterMap(HttpServletRequest request) {
|
||||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||||
String[] usernames = parameterMap.get("username");
|
String[] usernames = parameterMap.get("username");
|
||||||
String[] roles = parameterMap.get("roles");
|
String[] roles = parameterMap.get("roles");
|
||||||
UserDTO userDTO = new UserDTO();
|
UserDto userDto = new UserDto();
|
||||||
userDTO.setUsername(usernames[0]);
|
userDto.setUsername(usernames[0]);
|
||||||
userDTO.setRoles(Arrays.asList(roles));
|
userDto.setRoles(Arrays.asList(roles));
|
||||||
return userDTO;
|
return userDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/api/user4")
|
@GetMapping("/api/byParameterName")
|
||||||
public UserDTO byParameterName(String username, String[] roles) {
|
public UserDto byParameterName(String username, String[] roles) {
|
||||||
UserDTO userDTO = new UserDTO();
|
UserDto userDto = new UserDto();
|
||||||
userDTO.setUsername(username);
|
userDto.setUsername(username);
|
||||||
userDTO.setRoles(Arrays.asList(roles));
|
userDto.setRoles(Arrays.asList(roles));
|
||||||
return userDTO;
|
return userDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/api/user5")
|
@GetMapping("/api/byAnnoRequestParam")
|
||||||
public UserDTO byRequestParamAnnotation(@RequestParam("username") String var1, @RequestParam("roles") List<String> var2) {
|
public UserDto byAnnoRequestParam(@RequestParam("username") String var1, @RequestParam("roles") List<String> var2) {
|
||||||
UserDTO userDTO = new UserDTO();
|
UserDto userDto = new UserDto();
|
||||||
userDTO.setUsername(var1);
|
userDto.setUsername(var1);
|
||||||
userDTO.setRoles(var2);
|
userDto.setRoles(var2);
|
||||||
return userDTO;
|
return userDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/api/user6")
|
@GetMapping("/api/byPojo")
|
||||||
public UserDTO byPojo(UserDTO userDTO) {
|
public UserDto byPojo(UserDto userDto) {
|
||||||
return userDTO;
|
return userDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package com.baeldung.requestparam;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class UserDTO {
|
public class UserDto {
|
||||||
private String username;
|
private String username;
|
||||||
private List<String> roles;
|
private List<String> roles;
|
||||||
|
|
@ -1,5 +1,12 @@
|
|||||||
package com.baeldung.requestparam;
|
package com.baeldung.requestparam;
|
||||||
|
|
||||||
|
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.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
@ -9,12 +16,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
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
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
class QueryStringControllerIntegrationTest {
|
class QueryStringControllerIntegrationTest {
|
||||||
@ -29,36 +30,36 @@ class QueryStringControllerIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenInvokeGetQueryString_thenReturnTheOriginQueryString() throws Exception {
|
public void whenInvokeGetQueryString_thenReturnTheOriginQueryString() throws Exception {
|
||||||
this.mockMvc.perform(get("/api/user0?username=bob&roles=admin&roles=stuff"))
|
this.mockMvc.perform(get("/api/byGetQueryString?username=bob&roles=admin&roles=stuff"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().string("username=bob&roles=admin&roles=stuff"));
|
.andExpect(content().string("username=bob&roles=admin&roles=stuff"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenInvokeGetQueryParameter_thenReturnOneParameterValue() throws Exception {
|
public void whenInvokeGetQueryParameter_thenReturnOneParameterValue() throws Exception {
|
||||||
this.mockMvc.perform(get("/api/user1?username=bob"))
|
this.mockMvc.perform(get("/api/byGetParameter?username=bob"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().string("username:bob"));
|
.andExpect(content().string("username:bob"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenInvokeGetParameterValues_thenReturnParameterAsArray() throws Exception {
|
public void whenInvokeGetParameterValues_thenReturnParameterAsArray() throws Exception {
|
||||||
this.mockMvc.perform(get("/api/user2?roles=admin&roles=stuff"))
|
this.mockMvc.perform(get("/api/byGetParameterValues?roles=admin&roles=stuff"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().string("roles:[admin, stuff]"));
|
.andExpect(content().string("roles:[admin, stuff]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@CsvSource(textBlock = """
|
@CsvSource(textBlock = """
|
||||||
/api/user3
|
/api/byGetParameterMap
|
||||||
/api/user4
|
/api/byParameterName
|
||||||
/api/user5
|
/api/byAnnoRequestParam
|
||||||
/api/user6
|
/api/byPojo
|
||||||
""")
|
""")
|
||||||
public void whenPassParameters_thenReturnResolvedModel(String path) throws Exception {
|
public void whenPassParameters_thenReturnResolvedModel(String path) throws Exception {
|
||||||
this.mockMvc.perform(get(path + "?username=bob&roles=admin&roles=stuff"))
|
this.mockMvc.perform(get(path + "?username=bob&roles=admin&roles=stuff"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$.username").value("bob"))
|
.andExpect(jsonPath("$.username").value("bob"))
|
||||||
.andExpect(jsonPath("$.roles").value(containsInRelativeOrder("admin", "stuff")));
|
.andExpect(jsonPath("$.roles").value(containsInRelativeOrder("admin", "stuff")));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user