Resteasy Bean 验证在远程服务器上不起作用
Resteasy Bean Validation Not Working on Remote Server
我遇到的问题与 here 中描述的问题类似。
我在独立的 Jetty 应用程序中使用 RESTEasy。当我在本地启动应用程序并调用服务(例如 localhost:16880/rest/user/login)时,bean 验证工作正常,即我收到如下验证错误:
[PARAMETER]
[UserService#login(arg0).appKey]
[app_key may not be null or empty]
[]
但是,当我将应用程序部署到远程主机并调用相同的服务(例如 remotehost:16880/rest/user/login)时,根本不会调用 bean 验证.
我正在为服务使用 @ValidateRequest
注释,为 bean 参数使用 @Valid
注释。
我的 Resteasy 版本是 3.0.13.Final,尽管我也尝试过更早的版本。我已经尝试编写我的自定义验证器,但也没有用。
我很困惑为什么验证在本地有效,但在远程服务器上却无效。任何建议将不胜感激。
由于您将 Jetty 用作 独立 服务器,因此您必须在定义 ServletContextHandler
的位置定义 RESTEasy 验证提供程序。请注意,在独立服务器中,没有用于扫描 @Provider
类 和激活它们的容器,因此您必须手动执行此操作。
我希望您创建并启动您的服务器应用程序:
//create a server listening at some port
Server server= new Server(port);
//add server handlers
HandlerList handlers= new HandlerList();
initHandlers(handlers);
server.setHandler(handlers);
//start the server
server.start();
在 initHandlers
中您必须已经定义了您的 RESTEasy 支持:
public void initHandlers(List<HandlerList> handlers) {
//define root context handler
ServletContextHandler servletContextHandler= new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
handlers.addHandler(servletContextHandler);
//define RESTEasy handler
ServletHolder restServlet= new ServletHolder(new HttpServlet30Dispatcher());
//since this is a standalone server, somewhere you have to define RESTful services and Singletons
restServlet.setInitParameter("javax.ws.rs.Application", "com.exampleapp.MyRestApplication");
restServlet.setInitParameter("resteasy.servlet.mapping.prefix", "rest");
servletContextHandler.addServlet(restServlet, "rest/*");
}
所以现在剩下要做的就是添加验证提供程序作为初始参数:
restServlet.setInitParameter("resteasy.providers", "org.jboss.resteasy.plugins.validation.ValidatorContextResolver,org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper");
在这个 link 上,我试图找到验证程序提供者的名称:https://docs.jboss.org/resteasy/docs/3.0.4.Final/userguide/html/Validation.html
RESTEasy obtains a bean validation implemenation by looking in the available META-INF/services/javax.ws.rs.Providers files for an implementation of ContextResolver
所以它不说是什么,而是说在哪里。现在打开“resteasy-hibernatevalidator-provider-3...*.jar(来自 Eclipse -> Maven 依赖项或手动解压)并查看 META-INF/services/javax.ws.rs.ext.Providers 它说:
org.jboss.resteasy.plugins.validation.hibernate.ValidatorContextResolver
org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper
如果您没有此依赖项,请将其添加到您的 pom 文件中:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
请注意:在您描述验证提供程序的同一位置,如果您碰巧需要它们(例如 JacksonJaxbJson 等),您还可以添加其他提供程序。
我遇到的问题与 here 中描述的问题类似。
我在独立的 Jetty 应用程序中使用 RESTEasy。当我在本地启动应用程序并调用服务(例如 localhost:16880/rest/user/login)时,bean 验证工作正常,即我收到如下验证错误:
[PARAMETER]
[UserService#login(arg0).appKey]
[app_key may not be null or empty]
[]
但是,当我将应用程序部署到远程主机并调用相同的服务(例如 remotehost:16880/rest/user/login)时,根本不会调用 bean 验证.
我正在为服务使用 @ValidateRequest
注释,为 bean 参数使用 @Valid
注释。
我的 Resteasy 版本是 3.0.13.Final,尽管我也尝试过更早的版本。我已经尝试编写我的自定义验证器,但也没有用。
我很困惑为什么验证在本地有效,但在远程服务器上却无效。任何建议将不胜感激。
由于您将 Jetty 用作 独立 服务器,因此您必须在定义 ServletContextHandler
的位置定义 RESTEasy 验证提供程序。请注意,在独立服务器中,没有用于扫描 @Provider
类 和激活它们的容器,因此您必须手动执行此操作。
我希望您创建并启动您的服务器应用程序:
//create a server listening at some port
Server server= new Server(port);
//add server handlers
HandlerList handlers= new HandlerList();
initHandlers(handlers);
server.setHandler(handlers);
//start the server
server.start();
在 initHandlers
中您必须已经定义了您的 RESTEasy 支持:
public void initHandlers(List<HandlerList> handlers) {
//define root context handler
ServletContextHandler servletContextHandler= new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
handlers.addHandler(servletContextHandler);
//define RESTEasy handler
ServletHolder restServlet= new ServletHolder(new HttpServlet30Dispatcher());
//since this is a standalone server, somewhere you have to define RESTful services and Singletons
restServlet.setInitParameter("javax.ws.rs.Application", "com.exampleapp.MyRestApplication");
restServlet.setInitParameter("resteasy.servlet.mapping.prefix", "rest");
servletContextHandler.addServlet(restServlet, "rest/*");
}
所以现在剩下要做的就是添加验证提供程序作为初始参数:
restServlet.setInitParameter("resteasy.providers", "org.jboss.resteasy.plugins.validation.ValidatorContextResolver,org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper");
在这个 link 上,我试图找到验证程序提供者的名称:https://docs.jboss.org/resteasy/docs/3.0.4.Final/userguide/html/Validation.html
RESTEasy obtains a bean validation implemenation by looking in the available META-INF/services/javax.ws.rs.Providers files for an implementation of ContextResolver
所以它不说是什么,而是说在哪里。现在打开“resteasy-hibernatevalidator-provider-3...*.jar(来自 Eclipse -> Maven 依赖项或手动解压)并查看 META-INF/services/javax.ws.rs.ext.Providers 它说:
org.jboss.resteasy.plugins.validation.hibernate.ValidatorContextResolver org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper
如果您没有此依赖项,请将其添加到您的 pom 文件中:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
请注意:在您描述验证提供程序的同一位置,如果您碰巧需要它们(例如 JacksonJaxbJson 等),您还可以添加其他提供程序。