Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.List;
import java.util.Map;

import org.jboss.resteasy.reactive.common.jaxrs.ResponseImpl;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.common.model.ResourceClass;
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer;
Expand All @@ -21,18 +23,19 @@ public class SpringRequestParamHandler implements HandlerChainCustomizer {
@Override
public List<ServerRestHandler> handlers(HandlerChainCustomizer.Phase phase, ResourceClass resourceClass,
ServerResourceMethod resourceMethod) {
if (phase == Phase.AFTER_RESPONSE_CREATED) {
if (phase == Phase.RESOLVE_METHOD_PARAMETERS) {
return Collections.singletonList(new ServerRestHandler() {
@Override
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception {
Map<String, List<String>> parametersMap = requestContext.serverRequest().getQueryParamsMap();
if (parametersMap.isEmpty()) {
ResponseImpl response = (ResponseImpl) requestContext.getResponse().get();
response.setStatus(400);
throw new WebApplicationException("Missing required param in method '" + resourceMethod.getName() + "'",
Response.Status.BAD_REQUEST);
}
}
});
}
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import java.util.Optional;
import java.util.stream.Collectors;

import jakarta.ws.rs.WebApplicationException;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestController
@RequestMapping
Expand Down Expand Up @@ -69,4 +75,18 @@ public String updateFoos(@RequestParam MultiValueMap<String, String> allParams)
return result;
}

@GetMapping("/api/foos/paramRequired")
public String getFoosParamRequired(@RequestParam String id) {
throw new IllegalStateException("Unexpected state. Should have not been called");
}

@RestControllerAdvice
public static class RestExceptionHandler {

@ExceptionHandler(WebApplicationException.class)
public ResponseEntity<Object> handleException(Exception ex) {
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testSimpleMapping() throws Exception {
// In Spring, method parameters annotated with @RequestParam are required by default.
when().get("/api/foos")
.then()
.statusCode(400);
.statusCode(400).body(containsString("Missing required param in method"));
}

@Test
Expand Down Expand Up @@ -123,4 +123,12 @@ public void testMultiMap() throws Exception {
.statusCode(400);
}

@Test
public void shouldFailWithBadRequestWhenMissingMandatoryParam() throws Exception {
// In Spring, method parameters annotated with @RequestParam are required by default.
when().get("/api/foos/paramRequired")
.then()
.statusCode(400).body(containsString("Missing required param in method"));
}

}
Loading