java - JAXRS 2.0 Receive undefined number of parameters -
i'm considering expose jaxrs method receive undefined number of parameters:
so, i'd able handle like:
public class foo { property1, property2, list<keyvaluepair> ... } public class keyvaluepair { string key, string value }
then,
@post public response update(foo document) { (keyvaluepair pair : document.pairs) { ... } }
i have no idea achive this. i'll appreciate lot help. all.
mainly json. jee aplication using api libraries.the implementation depends of each container. actually, containers wildfly 8.2 , glassfish 4.1.
note solution json.
one way handle use jackson's @jsonanysetter
. can read more @ jackson tips: using @jsonanygetter/@jsonanysetter create "dyna beans". 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; } } }
any properties on model
aren't name
, put otherprops
map. due @jsonanysetter
annotation. @jsonanygetter
ensure the properties marshalled.
to use feature, portable way can suggest use add dependency
<dependency> <groupid>com.fasterxml.jackson.jaxrs</groupid> <artifactid>jackson-jaxrs-json-provider</artifactid> <version>2.4.0</version> </dependency>
glassfish doesn't have this, doesn't conflict anything. thing need disable default deserializer (moxy; trust me, you'll want anyway - jackson works better). disable moxy in portable way, set property. in application
class, 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; } }
this soft dependency, meaning there no classes needed use property. it's string. not affect other containers try use in. can leave property container, though strictly affects glassfish.
in wildfly, don't need else. "hardship" if port wildfly, should change above jackson dependency provided
<scope>
. wildfly uses dependency, under hood. not conflict it's version, can simple mark <scope>provided</scope>
. cannot glassfish, need include jars build.
Comments
Post a Comment