gpt4 book ai didi

glassfish - JAX-RS 自定义路径参数验证器

转载 作者:行者123 更新时间:2023-12-02 05:32:16 24 4
gpt4 key购买 nike

我目前正在尝试一些 RESTful JAX,我想验证自定义输入。通常正则表达式就可以了,但我需要进行更广泛的检查(大约 10 种不同的正则表达式模式)。我发现this page当搜索 jaxrs 验证时。我注意到它写着“草稿”,但我想我应该尝试一下。

我这样写了我的参数注释:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FooBarValidator.class)
public @interface FooBarParam
{
}

验证器如下所示:

@Provider
public class FooBarValidator
implements ConstraintValidator<FooBar, Long>
{
@Override
public void initialize(FooBar constraintAnnotation)
{
}

@Override
public boolean isValid(Long value, ConstraintValidatorContext context)
{
// validation goes here, this is a test validation
return (value > 50);
}
}

网络服务如下所示:

@javax.ejb.Stateless
@Path("test")
public class testRS
{
@GET
@Path("foobar/{fooBar: [0-9]+}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.TEXT_PLAIN)
public String testService(@FooBar @PathParam("fooBar") Long fooBar)
{
return "tested with: " + fooBar;
}
}

但是,如果我使用浏览器使用“http://localhost:8080/jaxtest/rest/test/foobar/11”调用 Web 服务,Web 服务就会被调用,并且会显示“tested with: 11” 。 Web 服务工作正常,只是验证器没有被调用。

我尝试在验证器类和注释接口(interface)中设置断点,但没有命中。

我隐隐怀疑我正在做一些不可能的事情,因为引用文档中的“草稿”标题。因此,如果我做错了什么或者有其他选择,我很高兴听到。

最佳答案

感谢 @PiotrKochański 给我的提示,我已经成功地完全实现了我想要的。最大的问题是我必须使用 Glassfish。默认情况下,Glassfish 使用 Jersey 来处理 JAX 内容。

我花了 10 多个小时才完成这个任务,所以对于任何偶然发现这个问题的人来说,这可以节省时间。

首先,使用 Maven,这会让你的生活变得更加轻松。

第二步,将 JBoss 存储库添加到您的 pom.xml

<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>

第三步,在pom.xml中添加依赖

<!-- Needed for validator interceptors -->
<dependency>
<groupId>org.jboss.seam.rest</groupId>
<artifactId>seam-rest</artifactId>
<version>3.1.0.Final</version>
</dependency>
<!-- JBoss' RS implementation -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.4.Final</version>
</dependency>
<!-- Because I use JSON I need RESTeasy be able to handle this -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.3.4.Final</version>
</dependency>
<!-- This is THE part that integrates validation in RESTeasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>2.3.4.Final</version>
</dependency>

最后一个依赖花了我相当长的时间。 The docs @PiotrKochański pointed to没有提到这一点。然而在 another version of the docs我发现了这个:

The integration between the API implementation and RESTEasy is done through the resteasy-hibernatevalidator-provider component. In order to integrate, we need to add resteasy-hibernatevalidator-provider and hibernate-validator to the classpath. With maven it's just a matter of including the following dependency:

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>2.3-RC1</version>
</dependency>

第四步是将其添加到 web.xml

<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>REST Service</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

第五步是修改 Web 服务类,如下所示:

@javax.ejb.Stateless
@Path("test")
public class testRS
{
@GET
@Path("foobar/{fooBar}")
@Produces(MediaType.APPLICATION_JSON)
@org.jboss.resteasy.spi.validation.ValidateRequest
public String testService(@FooBar @PathParam("fooBar") Long fooBar)
{
return "tested with: " + fooBar;
}
}

第六步是将@interface修改为:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FooBarValidator.class)
public @interface FooBarParam
{
String message() default "{constraint.FooBar}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}

同时作为奖励;我遇到了a presentation about Bean Validation by Emmanuel Bernard我想我可以分享一下,因为这解释了很多有趣的事情。

关于glassfish - JAX-RS 自定义路径参数验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11012821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com