Using custom object instead of @FormParam as input of JAX-RS resources
I have some example code:
@PUT
public String method(@FormParam("firstName") String firstName,
@FormParam("lastName") String lastName ) {
Person person = new Person(firstName, lastName);
// ...
}
I would like to stop using @FormParams and use Person instead:
@PUT
public String method(@Person person) {
// ...
}
What would be the best way to do this?
I am experimenting with a custom BodyReader, but I have to "manually" parse the InputStream's method readFrom
.
This is the correct way to do it, and if so, what is the way to convert the InputStream to KEY => VALUE HashMap?
source to share
there is @BeanParam option, see
https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e1905
source to share
You have two options:
- Perhaps you are only carrying one instance of an object inside that method, in which case you could simply avoid annotating the parameter
person
with any JAX-RS annotations. In the JAX-RS specification, this is called an entity parameter, and a REST method must have one such parameter, and then it is automatically decoded by registered entity providers. - Modify the class
person
so that its constructor receives one string as a parameter and is decoded in the constructor. Extracted from here :In general, the Java type of a method parameter can:
-
Be a primitive type; -Have a constructor that takes one String argument;
-
Have a static method named valueOf or fromString that takes a single String argument (see, for example, Integer.valueOf (String) and java.util.UUID.fromString (string));
-
Have a registered implementation of javax.ws.rs.ext.ParamConverterProvider JAX-RS SPI extension that returns a string-capable instance of javax.ws.rs.ext.ParamConverter for a type. or
-
List, Set, or SortedSet, where T satisfies 2 or 3 above. The resulting collection is read-only.
-
source to share