JAXRS 2.0接收未定义数量的参数 [英] JAXRS 2.0 Receive undefined number of parameters
问题描述
我正在考虑公开一个jaxrs方法,该方法接收到不确定数量的参数:
I'm considering to expose a jaxrs method that receive a undefined number of parameters:
所以,我希望能够处理类似的事情:
So, I'd like to be able to handle something like:
public class Foo {
property1,
property2,
List<KeyValuePair> ...
}
public class KeyValuePair {
String key,
String value
}
然后
@POST
public Response update(Foo document) {
for (KeyValuePair pair : document.pairs)
{
...
}
}
我不知道要实现这一目标. 非常感谢您的帮助. 谢谢大家.
I have no idea to achive this. I'll appreciate a lot your help. Thanks for all.
推荐答案
主要是JSON.我的JEE应用程序使用的是api库.实现取决于每个容器.实际上,我的容器是WildFly 8.2和GlassFish 4.1.
Mainly JSON. My JEE aplication is using api libraries.The implementation depends of each container. Actually, my containers are WildFly 8.2 and GlassFish 4.1.
注意:此解决方案仅适用于JSON.
Note this solution is only for JSON.
处理此问题的一种方法是使用Jackson的@JsonAnySetter
.您可以在 Jackson提示中了解有关此内容的更多信息:使用@ JsonAnyGetter/@ JsonAnySetter可以创建"dyna豆" .例如
One way to handle this is to use Jackson's @JsonAnySetter
. You can read more about it at Jackson tips: using @JsonAnyGetter/@JsonAnySetter to create "dyna beans". For example
@Path("json")
public class JsonResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response post(Model model) {
return Response.ok(model).build();
}
public static class Model {
public String name;
private Map<String, Object> otherProps = new HashMap<>();
@JsonAnySetter
public void anyProps(String key, Object value) {
otherProps.put(key, value);
}
@JsonAnyGetter
public Map<String, Object> otherProps() {
return otherProps;
}
}
}
Model
上所有不是name
的属性都放入了otherProps
映射中.这是由于@JsonAnySetter
注释引起的. @JsonAnyGetter
只是为了确保属性被编组.
Any properties on the Model
that aren't name
, are put into the otherProps
map. This is due to the @JsonAnySetter
annotation. The @JsonAnyGetter
is just to ensure the the properties get marshalled.
要使用此功能,我建议使用的最可移植的方法是添加此依赖项
To use this feature, the most portable way I can suggest to use it is to add this dependency
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.0</version>
</dependency>
Glassfish没有这个,所以它与任何东西都没有冲突.您唯一需要做的就是禁用默认的反序列化器(MOXy;相信我,您还是想这样做-Jackson更好地工作了).要以可移植的方式禁用MOXy,请设置属性.因此,在您的Application
类中,您可以拥有
Glassfish doesn't have this, so it doesn't conflict with anything. The only thing you need to do is disable the default deserializer (MOXy; trust me, you'll want to do this anyway - Jackson just works better). To disable MOXy in a portable way, you set the property. So in your Application
class, you can have
@ApplicationPath("/api")
public class JaxrsApplication extends Application {
@Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new HashMap<>();
properties.put("jersey.config.disableMoxyJson", true);
return properties;
}
}
这只是一个软依赖性,这意味着不需要任何类即可使用该属性.它只是一个字符串.它不会影响您尝试在其中使用的任何其他容器.因此,即使仅严格影响Glassfish,也可以将属性留给任何容器.
This is just a soft dependency, meaning there is no classes needed to use the property. It's simply a String. It will not affect any other containers you try to use it in. So you can leave the property for any container, even though is only strictly affects Glassfish.
在Wildfly中,您实际上不需要执行任何其他操作.我要说的唯一困难"是,如果移植到Wildfly,则应将上述Jackson依赖项更改为provided
<scope>
. Wildfly实际上已经在后台使用了这种依赖关系.为了不与它的版本冲突,我们可以简单地将其标记为<scope>provided</scope>
.我们无法使用Glassfish做到这一点,因为我们需要将罐子包含到构建中.
In Wildfly, you don't really need to do anything else. The only "hardship" I would say is that if you port to Wildfly, you should just change the above Jackson dependency to a provided
<scope>
. Wildfly actually already uses this dependency, under the hood. So as not to conflict with it's version, we can simple mark it as <scope>provided</scope>
. We cannot do this with Glassfish, as we need to include the jars into the build.
这篇关于JAXRS 2.0接收未定义数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!