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

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

Array keeps on adding same value in interceptor

问题

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

以下是拦截器的代码:

  1. public class LanguageInterceptor extends HandlerInterceptorAdapter {
  2. private List<String> errors = new ArrayList<>();
  3. private BadRequestException badRequestException = new BadRequestException();
  4. private BadRequestExceptionData badRequestExceptionData = new BadRequestExceptionData();
  5. @Override
  6. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  7. String localeLang = request.getHeader("Accept-Language");
  8. if (null == localeLang) {
  9. badRequestException.setStatus(HttpStatus.BAD_REQUEST.value());
  10. badRequestException.setTitle("Illegal request");
  11. errors.add("No language attribute specified in your request!");
  12. badRequestExceptionData.setPageSize(errors);
  13. badRequestException.setErrors(badRequestExceptionData);
  14. ObjectMapper objectMapper = new ObjectMapper();
  15. response.setStatus(HttpStatus.BAD_REQUEST.value());
  16. response.getWriter().write(objectMapper.writeValueAsString(badRequestException));
  17. response.setHeader("Content-Type", "application/json");
  18. return false;
  19. }
  20. return true;
  21. }
  22. }

示例响应:

  1. {
  2. "status": 400,
  3. "errors": {
  4. "pageSize": [
  5. "No language specified in your request!",
  6. "No language specified in your request!"
  7. ]
  8. },
  9. "title": "Illegal request"
  10. }

我是否做错了什么,还是在 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:

  1. public class LanguageInterceptor extends HandlerInterceptorAdapter {
  2. private List errors = new ArrayList();
  3. private BadRequestException badRequestException = new BadRequestException();
  4. private BadRequestExceptionData badRequestExceptionData = new BadRequestExceptionData();
  5. @Override
  6. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  7. String LocaleLang = request.getHeader(&quot;Accept-Language&quot;);
  8. if(null == LocaleLang) {
  9. badRequestException.setStatus(HttpStatus.BAD_REQUEST.value());
  10. badRequestException.setTitle(&quot;Illegal request&quot;);
  11. errors.add(&quot;No language attribute specified in your request!&quot;);
  12. badRequestExceptionData.setPageSize(errors);
  13. badRequestException.setErrors(badRequestExceptionData);
  14. ObjectMapper objectMapper = new ObjectMapper();
  15. response.setStatus(HttpStatus.BAD_REQUEST.value());
  16. response.getWriter().write(objectMapper.writeValueAsString(badRequestException));
  17. response.setHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
  18. return false;
  19. }
  20. return true;
  21. }
  22. }

Example response:

  1. {
  2. &quot;status&quot;: 400,
  3. &quot;errors&quot;: {
  4. &quot;pageSize&quot;: [
  5. &quot;No language specified in your request!&quot;,
  6. &quot;No language specified in your request!&quot;
  7. ]
  8. },
  9. &quot;title&quot;: &quot;Illegal request&quot;
  10. }

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 方法内部,使它们成为局部变量而不是实例变量。

  1. List errors = new ArrayList();
  2. BadRequestException badRequestException = new BadRequestException();
  3. 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.

  1. List errors = new ArrayList();
  2. BadRequestException badRequestException = new BadRequestException();
  3. 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:

确定