本文介绍了为什么自定义异常映射器中的实例变量必须是静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个自定义的异常映射器,以便将错误的请求抛出到我的应用程序中.这是代码:

I have implemented a custom exception mapper in order to throw the bad request in my application. Here's the code:

CustomFilterBadRequest:

CustomFilterBadRequest:

package com.test.exceptions;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import java.util.Date;

public class CustomFilterBadRequest extends Exception implements
        ExceptionMapper<CustomFilterBadRequest> {

    private String uriInfo;


    public CustomFilterBadRequest() {
        super("Invalid Request. Please try again with the valid request");
    }

    public CustomFilterBadRequest(String uriInfo, String message) {
        super(message);
        this.uriInfo = uriInfo;
    }

    @Override
    public Response toResponse(CustomFilterBadRequest exception) {
        return Response.status(400).entity(new ErrorDetails(new Date(),
                400, "bad request", exception.getMessage(),this.uriInfo)).type(MediaType.APPLICATION_JSON).build();
    }
}

问题:

注意:在此示例中,我有一个名为uriInfo的实例变量.从另一项服务中,我可以这样做:

Note:In this example, I have an instance variable called uriInfo. From another service, I do :

throw new CustomFilterBadRequest("uri","message")

立即调用构造函数,并将我的uriInfo设置为值"uri".之后,toResponse方法被调用.我的uriInfo实例变量被重置.为什么?而当我将实例变量设为静态时,该值将保留.

Immediately, the constructor gets called and my uriInfo is set to the value "uri". After this, toResponse method gets called.My uriInfo instance variable is reset. why? Whereas, when I made the instance variable to be static, the value is retained.

我无法获得任何文档.请帮忙.

I am unable to get any documentation on this. Please help.

*****更新*****

*****UPDATE*****

调用与此异常相关的REST端点时,将调用CustomFilterBadRequest的默认构造函数,以创建一个状态uriInfo初始化为null的对象.我们将此对象称为object1.

When the REST End point related to this exception is invoked, the default constructor of CustomFilterBadRequest is invoked creating an object with state uriInfo initialized to null. Let's call this object as object1.

当我们确实抛出新的CustomFilterBadRequest("uri","msg")时,将创建一个对象,其状态uriInfo初始化为值uri.我们将此对象称为object2.

when we do throw new CustomFilterBadRequest("uri","msg"), an object is created with state uriInfo initialized to the value uri. Let's call this object as object2.

JAX-RS运行时将CustomFilterBadRequest异常映射到异常映射器,此处也是CustomFilterBadRequest.因此,toResponse方法被调用.它从object1获取uriInfo. (此处显然为空)

JAX-RS Runtime maps the CustomFilterBadRequest exception to the exception mapper which is also CustomFilterBadRequest here. Hence toResponse method gets called. It gets uriInfo from object1. (which is obviously null here)

并且没有必要在自定义异常映射器中仅使用类变量.

And there's no necessity that we should use only class variables inside Custom exception mappers.

这种实现方式非常令人困惑.我在下面的答案中更新了简单的实现方式.

This kind of implementation is quite confusing. I have updated with the straightforward implementation in the answer below.

推荐答案

问题是,在已向JAX-RS运行时注册的CustomFilterBadRequest实例上调用了toResponse(...)方法.如果在注册中使用了no-arg构造函数,这就是uriInfo始终具有默认值null的原因.

The problem is that the toResponse(...) method is called on the instance of CustomFilterBadRequest that is registered with the JAX-RS runtime. If the no-arg constructor is used in registration that is why uriInfo always has it's default value of null.

我建议您在一个类中定义异常,而在另一类中定义异常处理程序.

I would recommend that you have your exception defined in one class, and you Exception handler defined in another.

例外:

public class CustomBadRequest extends Exception {

  private String uriInfo;

  public CustomBadRequest() {
    super("Invalid Request. Please try again with the valid request");
  }

  public CustomBadRequest(String uriInfo, String message) {
    super(message);
    this.uriInfo = uriInfo;
  }

  public String getUriInfo() {
    return uriInfo;
  }
}

ExceptionMapper:

ExceptionMapper:

@Provider
public class CustomFilterBadRequest implements ExceptionMapper<CustomBadRequest> {

  @Override
  public Response toResponse(CustomBadRequest exception) {
    return Response.status(400).entity(new ErrorDetails(new Date(),
      400, "bad request", exception.getMessage(),exception.getUriInfo())).type(MediaType.APPLICATION_JSON).build();
  }
}

这篇关于为什么自定义异常映射器中的实例变量必须是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 02:11