use of org.springframework.web.HttpRequestMethodNotSupportedException in project spring-framework by spring-projects.
the class RequestMappingInfoHandlerMappingTests method getHandlerRequestMethodNotAllowed.
@Test
public void getHandlerRequestMethodNotAllowed() throws Exception {
try {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar");
this.handlerMapping.getHandler(request);
fail("HttpRequestMethodNotSupportedException expected");
} catch (HttpRequestMethodNotSupportedException ex) {
assertArrayEquals("Invalid supported methods", new String[] { "GET", "HEAD" }, ex.getSupportedMethods());
}
}
use of org.springframework.web.HttpRequestMethodNotSupportedException in project CILManagement-Server by LiuinStein.
the class GlobalExceptionResolver method resolveException.
/**
* Resolve the exception form controller
*
* @param request http request
* @param response http response
* @param o the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
* @param exception the exception that threw from controller
* @return a new ModelAndView
* @implNote https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerExceptionResolver.html
*/
@NotNull
@Override
public ModelAndView resolveException(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @Nullable Object o, @NotNull Exception exception) {
RestfulResult result = new RestfulResult(1, exception.getMessage(), new HashMap<>());
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
if (exception instanceof SimpleException) {
result.setCode(((SimpleException) exception).getCode());
}
if (exception instanceof SimpleHttpException) {
response.setStatus(((SimpleHttpException) exception).getHttpStatusToReturn().value());
}
if (exception instanceof HttpRequestMethodNotSupportedException) {
result.setCode(405);
response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
}
if (exception instanceof ValidationException || exception instanceof ServletRequestBindingException || exception instanceof IllegalArgumentException) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
}
if (exception instanceof DataAccessException) {
result.setMessage("database access error");
}
try {
if ("application/xml".equals(request.getHeader("Accept"))) {
response.setHeader("Content-Type", "application/xml;charset=UTF-8");
response.getWriter().print(result.toXmlString());
} else {
response.setHeader("Content-Type", "application/json;charset=UTF-8");
response.getWriter().print(result.toJsonString());
}
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView();
}
use of org.springframework.web.HttpRequestMethodNotSupportedException in project spring-security-oauth by spring-projects.
the class DefaultWebResponseExceptionTranslator method translate.
@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
// Try to extract a SpringSecurityException from the stacktrace
Throwable[] causeChain = throwableAnalyzer.determineCauseChain(e);
Exception ase = (OAuth2Exception) throwableAnalyzer.getFirstThrowableOfType(OAuth2Exception.class, causeChain);
if (ase != null) {
return handleOAuth2Exception((OAuth2Exception) ase);
}
ase = (AuthenticationException) throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class, causeChain);
if (ase != null) {
return handleOAuth2Exception(new UnauthorizedException(e.getMessage(), e));
}
ase = (AccessDeniedException) throwableAnalyzer.getFirstThrowableOfType(AccessDeniedException.class, causeChain);
if (ase instanceof AccessDeniedException) {
return handleOAuth2Exception(new ForbiddenException(ase.getMessage(), ase));
}
ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer.getFirstThrowableOfType(HttpRequestMethodNotSupportedException.class, causeChain);
if (ase instanceof HttpRequestMethodNotSupportedException) {
return handleOAuth2Exception(new MethodNotAllowed(ase.getMessage(), ase));
}
return handleOAuth2Exception(new ServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), e));
}
use of org.springframework.web.HttpRequestMethodNotSupportedException in project spring-integration by spring-projects.
the class HttpInboundChannelAdapterParserTests method getRequestNotAllowed.
@Test
public void getRequestNotAllowed() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.setParameter("foo", "bar");
request.setRequestURI("/postOnly");
try {
this.integrationRequestMappingHandlerMapping.getHandler(request);
} catch (HttpRequestMethodNotSupportedException e) {
assertEquals("GET", e.getMethod());
assertArrayEquals(new String[] { "POST" }, e.getSupportedMethods());
}
}
use of org.springframework.web.HttpRequestMethodNotSupportedException in project thingsboard by thingsboard.
the class DefaultRestMsgHandler method process.
@Override
public void process(PluginContext ctx, PluginRestMsg msg) {
try {
log.debug("[{}] Processing REST msg: {}", ctx.getPluginId(), msg);
HttpMethod method = msg.getRequest().getMethod();
switch(method) {
case GET:
handleHttpGetRequest(ctx, msg);
break;
case POST:
handleHttpPostRequest(ctx, msg);
break;
case DELETE:
handleHttpDeleteRequest(ctx, msg);
break;
default:
msg.getResponseHolder().setErrorResult(new HttpRequestMethodNotSupportedException(method.name()));
}
log.debug("[{}] Processed REST msg.", ctx.getPluginId());
} catch (Exception e) {
log.warn("[{}] Exception during REST msg processing: {}", ctx.getPluginId(), e.getMessage(), e);
msg.getResponseHolder().setErrorResult(e);
}
}
Aggregations