数组在拦截器中不断添加相同的值。

huangapple go评论65阅读模式
英文:

Array keeps on adding same value in interceptor

问题

我试图做的是构建一个简单的拦截器,用于检查用户是否在标头中指定了语言属性。我已经进行了所有配置,并且一切都运行良好,但如果我提交请求超过一次,我会看到错误消息再次添加到数组中,现在显示两次。

以下是拦截器的代码:

public class LanguageInterceptor extends HandlerInterceptorAdapter {

    private List<String> errors = new ArrayList<>();
    private BadRequestException badRequestException = new BadRequestException();
    private BadRequestExceptionData badRequestExceptionData = new BadRequestExceptionData();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String localeLang = request.getHeader("Accept-Language");
        if (null == localeLang) {
            badRequestException.setStatus(HttpStatus.BAD_REQUEST.value());
            badRequestException.setTitle("Illegal request");
            errors.add("No language attribute specified in your request!");
            badRequestExceptionData.setPageSize(errors);
            badRequestException.setErrors(badRequestExceptionData);

            ObjectMapper objectMapper = new ObjectMapper();
            response.setStatus(HttpStatus.BAD_REQUEST.value());
            response.getWriter().write(objectMapper.writeValueAsString(badRequestException));
            response.setHeader("Content-Type", "application/json");
            return false;
        }
        return true;
    }
}

示例响应:

{
    "status": 400,
    "errors": {
        "pageSize": [
            "No language specified in your request!",
            "No language specified in your request!"
        ]
    },
    "title": "Illegal request"
}

我是否做错了什么,还是在 preHandle 函数完成时必须清空对象?如果我的问题听起来有点迟钝,我向你道歉,我对Java和Spring Boot还不太熟悉。

英文:

What I am trying to do is build a simple interceptor that checks whether the user has specified a language attribute in the headers or not. I have configured everything and all is running well but if I submit the request more than one time, I see that the error message got added in the array again and now is showing twice.

This is the code for the interceptor:

public class LanguageInterceptor extends HandlerInterceptorAdapter {

    private List errors = new ArrayList();
    private BadRequestException badRequestException = new BadRequestException();
    private BadRequestExceptionData badRequestExceptionData = new BadRequestExceptionData();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String LocaleLang = request.getHeader(&quot;Accept-Language&quot;);
        if(null == LocaleLang) {
            badRequestException.setStatus(HttpStatus.BAD_REQUEST.value());
            badRequestException.setTitle(&quot;Illegal request&quot;);
            errors.add(&quot;No language attribute specified in your request!&quot;);
            badRequestExceptionData.setPageSize(errors);
            badRequestException.setErrors(badRequestExceptionData);

            ObjectMapper objectMapper = new ObjectMapper();
            response.setStatus(HttpStatus.BAD_REQUEST.value());
            response.getWriter().write(objectMapper.writeValueAsString(badRequestException));
            response.setHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
            return false;
        }
        return true;
    }
}

Example response:

{
    &quot;status&quot;: 400,
    &quot;errors&quot;: {
        &quot;pageSize&quot;: [
            &quot;No language specified in your request!&quot;,
            &quot;No language specified in your request!&quot;
        ]
    },
    &quot;title&quot;: &quot;Illegal request&quot;
}

Am I doing something wrong or do I have to empty the object when the preHandle function is done? My apologies if this question sounds a bit dull, I am still new to Java and Spring boot.

答案1

得分: 1

看起来 LanguageInterceptor 已经被实例化为一个单例。将这三个变量都移到 preHandle 方法内部,使它们成为局部变量而不是实例变量。

List errors = new ArrayList();
BadRequestException badRequestException = new BadRequestException();
BadRequestExceptionData badRequestExceptionData = new BadRequestExceptionData();
英文:

It looks like LanguageInterceptor has been instantiated as a singleton. Shift all these three variables inside the method, preHandle to make them local instead of instance variables.

List errors = new ArrayList();
BadRequestException badRequestException = new BadRequestException();
BadRequestExceptionData badRequestExceptionData = new BadRequestExceptionData();

答案2

得分: 0

LanguageInterceptor是一个单例bean,errors列表将保持每个请求的状态。

您可以通过在方法内部声明列表来解决此问题。

同样,我建议您对异常也采取相同的做法。

英文:

The LanguageInterceptor is a singleton bean, the errors List will keep the status on every request.

You can solve this issue by declaring the list inside the method.

Also, I recommend you do the same for the exceptions.

huangapple
  • 本文由 发表于 2020年4月7日 08:37:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61071058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定