Merge pull request #15663 from f0rb/BAEL-6080
BAEL-6080: Getting Query String Parameters from HttpServletRequest
This commit is contained in:
commit
75951a207f
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.requestparam;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
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
|
||||
public class QueryStringController {
|
||||
|
||||
@GetMapping("/api/byGetQueryString")
|
||||
public String byGetQueryString(HttpServletRequest request) {
|
||||
return request.getQueryString();
|
||||
}
|
||||
|
||||
@GetMapping("/api/byGetParameter")
|
||||
public String byGetParameter(HttpServletRequest request) {
|
||||
String username = request.getParameter("username");
|
||||
return "username:" + username;
|
||||
}
|
||||
|
||||
@GetMapping("/api/byGetParameterValues")
|
||||
public String byGetParameterValues(HttpServletRequest request) {
|
||||
String[] roles = request.getParameterValues("roles");
|
||||
return "roles:" + Arrays.toString(roles);
|
||||
}
|
||||
|
||||
@GetMapping("/api/byGetParameterMap")
|
||||
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/byParameterName")
|
||||
public UserDto byParameterName(String username, String[] roles) {
|
||||
UserDto userDto = new UserDto();
|
||||
userDto.setUsername(username);
|
||||
userDto.setRoles(Arrays.asList(roles));
|
||||
return userDto;
|
||||
}
|
||||
|
||||
@GetMapping("/api/byAnnoRequestParam")
|
||||
public UserDto byAnnoRequestParam(@RequestParam("username") String var1, @RequestParam("roles") List<String> var2) {
|
||||
UserDto userDto = new UserDto();
|
||||
userDto.setUsername(var1);
|
||||
userDto.setRoles(var2);
|
||||
return userDto;
|
||||
}
|
||||
|
||||
@GetMapping("/api/byPojo")
|
||||
public UserDto byPojo(UserDto userDto) {
|
||||
return userDto;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
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.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;
|
||||
|
||||
@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/byGetQueryString?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/byGetParameter?username=bob"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("username:bob"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvokeGetParameterValues_thenReturnParameterAsArray() throws Exception {
|
||||
this.mockMvc.perform(get("/api/byGetParameterValues?roles=admin&roles=stuff"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("roles:[admin, stuff]"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(textBlock = """
|
||||
/api/byGetParameterMap
|
||||
/api/byParameterName
|
||||
/api/byAnnoRequestParam
|
||||
/api/byPojo
|
||||
""")
|
||||
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")));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue