Tag: jersey 2.0

使用Jersey 2.0,如何为每个请求注册一个可绑定的实例?

…如果实例需要手动构建,可能是第三方工厂类? 以前,(泽西岛1.x),你会做这样的事情: public class MyInjectableProvider extends PerRequestTypeInjectableProvider { public MyInjectableProvider() { super(MyInjectable.class); } @Override public Injectable getInjectable(ComponentContext ic, Context context) { MyInjectable myInjectableInstance = //… return new Injectable() { @Override public MyInjectable getValue() { return myInjectableInstance; } }; } } 匿名本地类能够访问要在某个范围内返回的实例。 当您不使用具有默认构造函数的类时,这非常有用,但它们需要基于每个请求构建。 Jersey 2.0切换到HK2作为dependency injection框架,但是,迁移页面( https://jersey.java.net/documentation/latest/migration.html )没有提供这种绑定的示例,并且HK2文档没有提供使用AbstractBinder的示例。 为了进一步阐述,我正在尝试向我的资源提供资源本地的,与容器无关的JPA EntityManager实例。 这些必须从单件工厂类中获取,并且应该只针对单个“工作单元”,这在我的情况下是一个请求。 我知道有解决方法(只是注入工厂,或绑定到threadlocal),但我发现以前的解决方案优雅,并希望尽可能重新创建它。 编辑: 在仔细研究了HK2 javadocs之后,我发现类似的东西可以实现如下: public […]

HK2 IterableProvider命名方法未找到实现

我试图注入一个与它绑定的两个服务的合同时遇到问题。 我正在使用Jersey,并扩展ResourceConfig来配置我的应用程序,我将两个不同的实现(类FooImpl1和FooImpl2 )绑定到同一个合同(接口Foo ),对它们进行不同的排名。 这些实现中的每一个都使用@Named及其名称进行注释。 在我的一个控制器中,我想要访问这两个实现,所以我注入了一个IterableProvider fooProvider 。 如果我没有指定任何内容,总是会注入具有最高级别的实现,这就是我想要的。 当我想要一个具体的实现时,问题出现了,其中之一。 当我调用fooProvider.named( nameOfTheClass ).get() ,返回null,但如果我遍历fooProvider,我可以访问这两个实现,因此它们被注入。 任何人都知道我能错过什么? 非常感谢你的帮助。

在独立应用程序中使用Jersey的dependency injection

我这里有一个界面 interface Idemo{ public int getDemo(int i); } 这是一个实现 class DemoImpl implements Idemo{ @Override public int getDemo(int i){ return i+10; } } 并且有一个类依赖于Idemo class Sample{ @Inject Idemo demo; public int getSample(int i){ return demo.getDemo(i); } } 现在说我想测试Sample类 public class SampleTest extends JerseyTest { @Inject Sample s; @Override protected Application configure() { AbstractBinder binder = new […]

Jersey 2.26:在ResourceConfig中注册@Inject bindFactory无法将Factory转换为Supplier

我现在正在使用Jersey,我想将一个GeneralForm映射注入到Resource类上下文中,该上下文接受所有application/json , multipart/form-data和application/x-www-form-urlencoded格式提交。 我按照Jersey文档中指定的说明操作: https://jersey.github.io/documentation/latest/ioc.html#d0e17033 GeneralForm.java package cn.easecloud.jrf.provider; import java.util.HashMap; public class GeneralForm extends HashMap { } GeneralFormFactory.java package cn.easecloud.jrf.provider; import org.glassfish.hk2.api.Factory; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; public class GeneralFormFactory implements Factory { private final HttpServletRequest request; @Inject public GeneralFormFactory(HttpServletRequest request) { this.request = request; } @Override public GeneralForm provide() { GeneralForm result = new GeneralForm(); […]

基于没有单例的HttpRequest的jersey 2上下文注入

我想按字段为单个请求注入一个数据存储区,比如 @Context protected HttpServletRequest request; 目前我已经实现了类似的方法: Jersey 2.x自定义注入注释具有属性如下: @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.FIELD}) public @interface TenantDatastore {} public class TenantDatastoreFactory extends AbstractContainerRequestValueFactory { public TenantDatastoreFactory() {} @Override public Datastore provide() { ContainerRequest request = getContainerRequest(); return DatastoreManager.getDs(request.getHeaders().get(“Host”))); } @Override public void dispose(Datastore d) {} } public class TenantDatastoreFactoryProvider extends AbstractValueFactoryProvider { private final TenantDatastoreFactory tenantDatastoreFactory; @Inject public […]

如何获取javax.ws.rs.core.UriInfo的实例

是否有任何javax.ws.rs.core.UriInfo实现,我可以用它来快速创建一个实例进行测试。 这个界面很长,我只需要测试一下。 我不想在这个界面的整个实现上浪费时间。 更新:我想为类似于此的函数编写unit testing: @GET @Path(“/my_path”) @Produces(MediaType.TEXT_XML) public String webserviceRequest(@Context UriInfo uriInfo);

使用Jersey 2.21在REST API请求中的可选参数

我正在玩Jersey 2.21 ,我想知道是否可以有一个“可选”的参数,它可以或不存在于对服务器的请求中。 我想成功访问这两种方法: http://localhost:8080/my_domain/rest/api/myMethod/1 http://localhost:8080/my_domain/rest/api/myMethod 正如您所看到的,我正在尝试使整数( id )参数成为可选参数。 我已经将myMethod声明如下: @GET @Path(“myMethod/{id}”) @Produces(MediaType.APPLICATION_JSON + “;charset=UTF-8”) public String myMethod(@PathParam(“id”) Integer id, @Context HttpHeaders hh) 这有效: http://localhost:8080/my_domain/rest/api/myMethod/1 这也有效: http://localhost:8080/my_domain/rest/api/myMethod/ 但这不起作用 ,我不明白为什么。 它抛出404 Not Found错误: http://localhost:8080/my_domain/rest/api/myMethod 你能指出我正确的方向来解决这个问题吗? 我不喜欢斜线在我所有的REST方法调用中都是强制性的,并且如果可能的话想要压缩它。

内省泽西资源模型泽西2.x.

我编写了自己的扫描程序来浏览我的JAX-RS资源,并使用jersey-server-1.18.1打印出方法名称和路径。 问题是当我将相同的代码迁移到2.16(将包名称从com.sun.*更改为org.glassfish.* )时,它将无法正常工作。 深入挖掘我发现那些必需的jersey-server类不久就公开了。 谁知道原因? 如何将我的代码从1.x迁移到2.x? 实际上没有关于此迁移的文档。 所有帮助赞赏! 下面是1.x的代码 import com.wordnik.swagger.annotations.ApiOperation; import com.sun.jersey.api.model.AbstractResource; import com.sun.jersey.api.model.AbstractResourceMethod; import com.sun.jersey.api.model.AbstractSubResourceLocator; import com.sun.jersey.api.model.AbstractSubResourceMethod; import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * […]

我可以在ConstraintValidator中为Method参数更改属性路径吗?

如果您熟悉Beanvalidation框架,则您知道无法获取方法参数的名称。 因此,如果对方法的第一个参数执行@NotNull约束并且validation失败,则getPropertyPath将类似于“arg1”。 我想创建我自己的@NotNull版本,它可以取一个值,例如@NamedNotNull(“emailAddress”)。 但我无法弄清楚如何覆盖我的Validator中的#getPropertyPath? 有没有办法做到这一点,或者我坚持使用“arg1”或“arg2”等。 编辑 根据我收到的答案,我能够提出以下实现,允许我从@QueryParam或@PathParam注释中获取值,并将其用作Beanvalidation注释(如@NotNull)的属性路径。 对于Jersey,您需要创建以下类。 注意DefaultParameterNameProvider的实现: public class ValidationConfigurationContextResolver implements ContextResolver { @Override public ValidationConfig getContext( final Class type ) { final ValidationConfig config = new ValidationConfig(); config.parameterNameProvider( new RestAnnotationParameterNameProvider() ); return config; } static class RestAnnotationParameterNameProvider extends DefaultParameterNameProvider { @Override public List getParameterNames( Method method ) { Annotation[][] annotationsByParam = method.getParameterAnnotations(); […]

带有FormDataContentDisposition的org.glassfish.jersey上传文件

http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/我正在按照本指南操作并遇到问题。 我有一些疑问。 所有的依赖都必须对应吗? 我的项目有一些org.glassfish.jersey依赖项,本指南建议使用org.sun.jersey。 我是否必须使用与此相同的版本进行更改? org.glassfish.jersey.media jersey-media-multipart 2.16 org.glassfish.jersey.core jersey-server 2.16 我有这个错误 org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] No injection source found for a parameter of type public ***.***.****.common.dto.response.AbstractResponse ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source=’ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@90516e]}, definitionMethod=public ***.***.***.common.dto.response.AbstractResponse ***.***.*****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, […]