简体   繁体   English

ExceptionHandlerExceptionResolver在Spring 5中始终返回200个响应代码

[英]ExceptionHandlerExceptionResolver always returns 200 response code in Spring 5

I have upgraded from Spring 4.2 to Spring 5.0.5. 我已经从Spring 4.2升级到Spring 5.0.5。 After upgrading my junits are failing with the below error. 升级后,我的Junits失败,并出现以下错误。

java.lang.AssertionError: Testing return value. expected:<405> but was:<200>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at 

Below is my code: 下面是我的代码:

`package com.hp.ci.mgmt.controllers;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import 
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import 

org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver; org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;


public class AbstractBaseControllerTest extends AbstractBaseController
{
ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
ObjectMapper mapper = new ObjectMapper();

private MockHttpServletRequest request;
private MockHttpServletResponse response;

@Mock
CiException cie;

@SuppressWarnings("rawtypes")
@Before
public void setup()
{
    final List<HttpMessageConverter<?>> messageConvertersList = new ArrayList<HttpMessageConverter<?>>();
    messageConvertersList.add(new MappingJackson2HttpMessageConverter());
    resolver.setMessageConverters(messageConvertersList);
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();

    MockitoAnnotations.initMocks(this);
    Mockito.when(cie.getErrorSource()).thenReturn("test");
}

@Test
public void testUnsupportedOperationException()
        throws Exception
{
    validateException(HttpStatus.METHOD_NOT_ALLOWED, new RequestNotAllowedException());
}

private void validateException(final HttpStatus expectedStatus, final 
Exception e)
        throws Exception
{
    validateExceptionStatus(expectedStatus, e);        
}

 private void validateExceptionStatus(final HttpStatus expectedStatus, final Exception e)
        throws Exception
{
    resolver.resolveException(request, response, this, e);
    if (expectedStatus == null)
    {
        assertEquals("Testing return value.", HttpStatus.BAD_REQUEST.value(), response.getStatus());
    }
    else
    {
        assertEquals("Testing return value.", expectedStatus.value(), response.getStatus());
    }

}

Issue is that the below code always returns 200 status code, therefore all my tests fail. 问题是以下代码始终返回200状态代码,因此我的所有测试均失败。

resolver.resolveException(request, response, this, e); resolver.resolveException(request,response,this,e);

Does anyone know how to resolve this issue? 有谁知道如何解决这个问题?

I found what is causing the issue. 我找到了导致问题的原因。 Looks like "this" in the below line does not extend HandlerMethod so resolveException returns null without doing anything. 看起来像下面一行中的“ this”没有扩展HandlerMethod,所以resolveException不做任何事情就返回null。

resolver.resolveException(request, response, this, e); resolver.resolveException(request,response,this,e);

@Override
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object 
handler) {
    if (handler == null) {
        return super.shouldApplyTo(request, null);
    }
    else if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        handler = handlerMethod.getBean();
        return super.shouldApplyTo(request, handler);
    }
    else {
        return false;
    }
}

Can someone help me with configuring the handler. 有人可以帮助我配置处理程序。 How can i implement HandlerMethod? 如何实现HandlerMethod?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 响应代码总是给出 200 - response code always give 200 改造2-响应正文返回null(响应代码为200) - Retrofit 2 - response body returns null (response code is 200) Spring Boot - Jackson EntityNotFoundException 返回 200 而不是 500 响应 - Spring Boot - Jackson EntityNotFoundException returns 200 instead of 500 response spring 引导返回 200 并带有“OK”错误属性附加到正常的主体响应 - spring boot returns 200 with “OK” ErrorAttributes attached to normal body response 调用请求 localhost 总是返回状态码 200 - Calling request localhost always returns status code 200 如何在 Java 代码中声明 ExceptionHandlerExceptionResolver? - How to declare ExceptionHandlerExceptionResolver in Java code? WebSocket 握手 - 意外响应代码 200 - AngularJs 和 Spring Boot - WebSocket Handshake - Unexpected response code 200 - AngularJs and Spring Boot HttpURLConnection总是以模拟方式返回200 - HttpURLConnection returns 200 always in mockito Spring 安全登录始终为 200 - Spring security login always 200 为什么 ExceptionHandlerExceptionResolver 进行重定向而不是简单响应? - Why ExceptionHandlerExceptionResolver makes redirect instead of simple response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM