且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将JAX-RS bean验证错误消息绑定到视图

更新时间:2021-10-10 17:34:45

编辑1

我们引入了新的注释 @ ErrorTemplate 涵盖了这个用例。 使用JAX-RS和Bean验证错误处理MVC 更深入地描述了它并展示了如何使用它。

We introduced new annotation @ErrorTemplate in Jersey 2.3 that covers this use-case. Handling JAX-RS and Bean Validation Errors with MVC describes it deeper and shows how to use it.

使用Jersey你可以按照以下步骤操作:

With Jersey you can follow these steps:


  1. 添加以下依赖项: jersey-bean-validation jersey-mvc-jsp

  2. 创建的/rs/ext/ExceptionMapper.html\">ExceptionMapper ConstraintViolationException

  3. 注册您的提供者

  1. add the following dependencies: jersey-bean-validation and jersey-mvc-jsp
  2. create an ExceptionMapper for ConstraintViolationException
  3. register your providers



依赖关系



如果你正在使用Maven,你只需将这些依赖项添加到 pom.xml

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-mvc-jsp</artifactId>
    <version>2.1</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-bean-validation</artifactId>
    <version>2.1</version>
</dependency>

否则请参阅模块依赖关系页面以获取所需库的列表( jersey-mvc-jsp jersey-bean-validation )。

otherwise refer to the modules dependency pages to get a list of required libraries (jersey-mvc-jsp and jersey-bean-validation).

在验证实体(或JAX-RS资源)期间出现错误时,Bean Validation运行时抛出ConstraintViolationException。 Jersey 2.x提供了一个标准的ExceptionMapper来处理这些异常(确切地说是 ValidationException )所以如果你想以不同的方式处理它们,你需要编写一个自己的ExceptionMapper:

Bean Validation runtime throws a ConstraintViolationException when something goes wrong during validation an entity (or JAX-RS resource). Jersey 2.x provides a standard ExceptionMapper to handle such exceptions (ValidationException to be precise) so if you want to handle them differently, you need to write an ExceptionMapper of your own:

@Provider
@Priority(Priorities.USER)
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {

    @Override
    public Response toResponse(final ConstraintViolationException exception) {
        return Response
                // Define your own status.
                .status(400)
                // Put an instance of Viewable in the response so that jersey-mvc-jsp can handle it.
                .entity(new Viewable("/error.jsp", exception))
                .build();
    }
}

使用上面的ExceptionMapper,您将处理所有抛出的内容ConstraintViolationExceptions和最终响应将具有 HTTP 400 响应状态。实体通过(可查看)到响应将是由MessageBodyWriter从 jersey-mvc 模块处理,它基本上将输出一个已处理的JSP页面。 Viewable类的第一个参数是JSP页面的路径(您可以使用相对路径或绝对路径),第二个参数是JSP将用于渲染的模型(模型可通过 $ {it访问JSP中的属性)。有关此主题的更多信息,请参阅泽西用户指南中有关 MVC 的部分。

With the ExceptionMapper above you'll be handling all thrown ConstraintViolationExceptions and the final response will have HTTP 400 response status. Entity passed (Viewable) to the response will be processed by MessageBodyWriter from jersey-mvc module and it will basically output a processed JSP page. The first parameter of the Viewable class is a path to a JSP page (you can use relative or absolute path), the second is the model the JSP will use for rendering (the model is accessible via ${it} attribute in JSP). For more on this topic, refer to the section about MVC in Jersey Users guide.

您需要做的最后一步是将您的提供商注册到应用程序(我将使用ResourceConfig 扩展了应用类):

The last step you need to make is to register your providers into your Application (I'll show you an example using ResourceConfig from Jersey which extends Application class):

new ResourceConfig()
    // Look for JAX-RS reosurces and providers.
    .package("my.package")
    // Register Jersey MVC JSP processor.
    .register(JspMvcFeature.class)
    // Register your custom ExceptionMapper.
    .register(ConstraintViolationExceptionMapper.class)
    // Register Bean Validation (this is optional as BV is automatically registered when jersey-bean-validation is on the classpath but it's good to know it's happening).
    .register(ValidationFeature.class);