2

I have a localService which I want to access via our restful api:

@GET
@Path("/some/path")
OutputObject doSomeSpecialCalculation(@QueryParam("input") InputObject obj);

Following problem/problems - what would be the best approach:

  • To be able to create the InputObject, I need a lot of different input data -> the consturctor looks something like that: new InputObject(Obj1 o1, Obj2 o2, ..., Obj6 o6);

Q1: Is it possible to map many queryParams into a single object?

  • The next problem is that most of the parameters (obj_n) need a special ParamProvider for construction. (e.g. the rest call for Obj2 o2 is an id number and the ParamConverter maps the id to the correct object)

I could create a new local service method like that:

@GET
@Path("/some/path")
OutputObject doSomeSpecialCalculation(@QueryParam("obj1") Obj1 ob1, @QueryParam("obj2") Obj2 ob2, ...);

Then I could create multiple ParamProviders for each obj_n and it would work, but I don't want to create duplicated methods in our localservices.

Q2: Would there be a better solution for my particular problem?

TL;DR:

  • I have a local service method which takes a ComplexObject as an input parameter
  • I want to access that method via a rest call
  • To create that ComplexObject I need multiple objects as parameters for the constructor
  • The most of the multiple parameters need a special mapper/converter: Restful-Input: id -> Object

It would be awesome if I could solve this problem just with annotations: @JsonTypeInfo on the complex object, and some "use-that-converter"-annotations on the input objects of the complex object's constructor.

Regards,

(using jackson 1.9/jboss eap 6.2)

1 Answer 1

1

Use @BeanParam annotation on the Endpoint's method argument (your custom class) and use all the needed @QueryParam, @Header etc. values on fields of the custom class.

And this is how it looks like for a POST with JSON:

JSON:

{
    "user_name" : "Chewbacca",
    "year_of_birth" : 1977
}

Java:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

public class SimpleRequest {

    @NotNull
    private final String userName;

    @Min(1900)
    private final int yearOfBirth;

    @JsonCreator
    public SimpleRequest(@JsonProperty("user_name") String userName,
                         @JsonProperty("year_of_birth") int yearOfBirth) {
        this.userName = userName;
        this.yearOfBirth = yearOfBirth;
    }

    public String getUserName() {
        return userName;
    }

    public int getYearOfBirth() {
        return yearOfBirth;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, just researched that approach and it looks quite good. Could work for GET requests with a complex parameter. Do you also have an approach for a Post ('someValue method(ComplexObject obj)), where the ComplexObject received through the posted body as json. Currently I am parsing the json by hand with a MessageBodyWriter implementation, but it would be much cleaner if I could just provide a mapper who could transform json into a ComplexObject. And if a ComplexObject would use ComplexObject1 as a member variable, the mapper would search for a mapper for ComplexObject1
Sure, see my other answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.