Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 结构化查询参数的JAX-RS映射_Java_Jax Rs_Dropwizard - Fatal编程技术网

Java 结构化查询参数的JAX-RS映射

Java 结构化查询参数的JAX-RS映射,java,jax-rs,dropwizard,Java,Jax Rs,Dropwizard,我们的查询参数有一个结构(实际上,它们形成了一个映射),这导致了JAX-RS绑定中的重复 我们可以更改绑定以消除重复吗 @GET @Produces("application/json;charset=UTF-8") List<BoundThings> getSomeBoundThings( @PathParam("id") UUID id, @QueryParam("bindings.organisation") Bindings orgBindin

我们的查询参数有一个结构(实际上,它们形成了一个
映射
),这导致了JAX-RS绑定中的重复

我们可以更改绑定以消除重复吗

@GET @Produces("application/json;charset=UTF-8")
List<BoundThings> getSomeBoundThings(
        @PathParam("id") UUID id,
        @QueryParam("bindings.organisation") Bindings orgBindings,
        @QueryParam("bindings.person") Bindings personBindings,
        @QueryParam("bindings.location") Bindings locBindings,
        @QueryParam("bindings.brand") Bindings brandBindings,
        @QueryParam("bindings.genre") Bindings genreBindings,
        @QueryParam("bindings.icb") Bindings icbBindings,
        @QueryParam("bindings.iptc") Bindings iptcBindings,
        @QueryParam("bindings.section") Bindings sectionBindings,
        @QueryParam("bindings.subject") Bindings subjBindings,
        @QueryParam("bindings.topic") Bindings topicBindings,
        @QueryParam("bindings.specialreport") Bindings rptBindings) {
}
@GET@products(“application/json;charset=UTF-8”)
列出你想要的东西(
@路径参数(“id”)UUID id,
@QueryParam(“bindings.organization”)绑定orgBindings,
@QueryParam(“bindings.person”)绑定personBindings,
@QueryParam(“bindings.location”)绑定locBindings,
@QueryParam(“bindings.brand”)绑定brandBindings,
@QueryParam(“bindings.genre”)绑定genreBindings,
@QueryParam(“bindings.icb”)绑定icbBindings,
@QueryParam(“bindings.iptc”)绑定iptcBindings,
@QueryParam(“bindings.section”)绑定节绑定,
@QueryParam(“bindings.subject”)绑定subbindings,
@QueryParam(“bindings.topic”)绑定topicBindings,
@QueryParam(“bindings.specialreport”)绑定(rptBindings){
}
仅针对上下文,该方法查找由UUID标识的特定实体的元数据。Bindings是标识要使用的绑定类型的枚举。每个绑定定义一个关系


是否可以使用
Map
?如何清理?

有几种清理方法

注意:这仅适用于DW[8.0,)

你可以。。。 只需注入
UriInfo
并获得一个
MultivaluedMap
(尽管您无法获得
MultivaluedMap

你可以。。。 创建一个类似映射的bean类,并创建一个
工厂
[1],这样您就可以使用
@Context
注入它

地图包装器

public class BindingsMap {

    public static final String BINDINGS_ORGANIZATION = "bindings.organisation";
    public static final String BINDINGS_PERSON = "bindings.person";

    private final Map<String, Bindings> bindings = new HashMap<>();

    public void put(String key, Bindings bindings) {
        this.bindings.put(key, bindings);
    }

    public Bindings get(String key) {
        return this.bindings.get(key);
    }
}
然后你需要注册工厂

env.jersey().register(new AbstractBinder(){
    @Override
    public void configure() {
        bindFactory(QueryBindingsFactory.class)
                .to(BindingsMap.class)
                .in(RequestScoped.class);
    }
});
[1]参见

你可以。。。 如果您不喜欢
@Context
注释,请创建一个自定义注释以插入上述
绑定映射。您可以在中看到完整的示例。它几乎只是构建在上述选项之上。它需要额外的:

  • 自定义注释
  • 一个
    注入解析器
  • 和一个
    AbstractValueFactoryProvider

  • 所有示例都可以在链接中看到。如果您对
    @Context
    注释没有意见,那么使用该注释可能更简单。

    您可以对所有GET参数进行Json编码,然后使用类似Jackson的库对其进行解码。
    public class BindingsMap {
    
        public static final String BINDINGS_ORGANIZATION = "bindings.organisation";
        public static final String BINDINGS_PERSON = "bindings.person";
    
        private final Map<String, Bindings> bindings = new HashMap<>();
    
        public void put(String key, Bindings bindings) {
            this.bindings.put(key, bindings);
        }
    
        public Bindings get(String key) {
            return this.bindings.get(key);
        }
    }
    
    public class QueryBindingsFactory
            extends AbstractContainerRequestValueFactory<BindingsMap> {
    
        @Override
        public BindingsMap provide() {
            BindingsMap bindingsMap = new BindingsMap();
            put(bindingsMap, BindingsMap.BINDINGS_ORGANIZATION);
            put(bindingsMap, BindingsMap.BINDINGS_PERSON);
            return bindingsMap;
        }
    
        private void put(BindingsMap bindingsMap, String key) {
            ContainerRequest request = getContainerRequest();
            MultivaluedMap<String, String> queryParams
                    = request.getUriInfo().getQueryParameters();
            bindingsMap.put(key, Bindings.fromString(queryParams.getFirst(key)));
        }
    
        @Override
        public void dispose(BindingsMap t) {
        }
    }
    
    @GET
    public Response get(@Context BindingsMap bindingsMap) {}
    
    env.jersey().register(new AbstractBinder(){
        @Override
        public void configure() {
            bindFactory(QueryBindingsFactory.class)
                    .to(BindingsMap.class)
                    .in(RequestScoped.class);
        }
    });