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; | ||||
| 
 | ||||
| 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; | ||||
| 
 | ||||
| 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/user0") | ||||
|     @GetMapping("/api/byGetQueryString") | ||||
|     public String byGetQueryString(HttpServletRequest request) { | ||||
|         return request.getQueryString(); | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/api/user1") | ||||
|     @GetMapping("/api/byGetParameter") | ||||
|     public String byGetParameter(HttpServletRequest request) { | ||||
|         String username = request.getParameter("username"); | ||||
|         return "username:" + username; | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/api/user2") | ||||
|     @GetMapping("/api/byGetParameterValues") | ||||
|     public String byGetParameterValues(HttpServletRequest request) { | ||||
|         String[] roles = request.getParameterValues("roles"); | ||||
|         return "roles:" + Arrays.toString(roles); | ||||
|     } | ||||
| 
 | ||||
|     @GetMapping("/api/user3") | ||||
|     public UserDTO byGetParameterMap(HttpServletRequest request) { | ||||
|     @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; | ||||
|         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/byParameterName") | ||||
|     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/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/user6") | ||||
|     public UserDTO byPojo(UserDTO userDTO) { | ||||
|         return userDTO; | ||||
|     @GetMapping("/api/byPojo") | ||||
|     public UserDto byPojo(UserDto userDto) { | ||||
|         return userDto; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -2,7 +2,7 @@ package com.baeldung.requestparam; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class UserDTO { | ||||
| public class UserDto { | ||||
|     private String username; | ||||
|     private List<String> roles; | ||||
| 
 | ||||
| @ -1,5 +1,12 @@ | ||||
| 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; | ||||
| @ -9,12 +16,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock | ||||
| 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 { | ||||
| @ -29,36 +30,36 @@ class QueryStringControllerIntegrationTest { | ||||
| 
 | ||||
|     @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")); | ||||
|         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/user1?username=bob")) | ||||
|                     .andExpect(status().isOk()) | ||||
|                     .andExpect(content().string("username:bob")); | ||||
|         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/user2?roles=admin&roles=stuff")) | ||||
|                     .andExpect(status().isOk()) | ||||
|                     .andExpect(content().string("roles:[admin, stuff]")); | ||||
|         this.mockMvc.perform(get("/api/byGetParameterValues?roles=admin&roles=stuff")) | ||||
|             .andExpect(status().isOk()) | ||||
|             .andExpect(content().string("roles:[admin, stuff]")); | ||||
|     } | ||||
| 
 | ||||
|     @ParameterizedTest | ||||
|     @CsvSource(textBlock = """ | ||||
|             /api/user3 | ||||
|             /api/user4 | ||||
|             /api/user5 | ||||
|             /api/user6 | ||||
|             """) | ||||
|         /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"))); | ||||
|             .andExpect(status().isOk()) | ||||
|             .andExpect(jsonPath("$.username").value("bob")) | ||||
|             .andExpect(jsonPath("$.roles").value(containsInRelativeOrder("admin", "stuff"))); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user