eclipse / microprofile-rest-client

MicroProfile Rest Client
Apache License 2.0
142 stars 72 forks source link

Allow dual-use JAXB/JSON-B xml/json & `@BeanParam` form objects #117

Open derekm opened 6 years ago

derekm commented 6 years ago

Allow objects to work with JAXB/JSON-B & @BeanParam.

Allowing dual-use POJOs helps unify JAX-RS resource definitions.

Consider two services, a GET and a POST. In order to support both JSON/XML and x-www-form-urlencoded, two POST definitions are currently needed (in recent version of Jersey). E.g.,

/**
 * Accepts answers to questions that map to Standard Data Set attributes.
 *
 * @author Derek Moore <a href="mailto:dmoore@fanthreesixty.com">dmoore@fanthreesixty.com</a>
 */
public interface AnswersResource {

    @GET
    @Consumes({
            MediaType.APPLICATION_JSON,
            MediaType.APPLICATION_XML,
            })
    @Produces({
            MediaType.APPLICATION_JSON,
            MediaType.APPLICATION_XML,
            })
    public List<QuestionAnswer> get(@QueryParam("flowId") UUID flowId);

    /**
     * Post an answer to a Standard Data Set question.
     * @param answer an answer to a Standard Data Set question
     * @return the answer as saved
     */
    @POST
    @Consumes({
            MediaType.APPLICATION_JSON,
            MediaType.APPLICATION_XML,
            })
    @Produces({
            MediaType.APPLICATION_JSON,
            MediaType.APPLICATION_XML,
            })
    public QuestionAnswer post(QuestionAnswer answer);

    /**
     * Form post an answer to a Standard Data Set question.
     * @param flowId the flow the question is in
     * @param orgId the id for the org that the question belongs to
     * @param customerId the customer answering the question
     * @param attributeKey the attribute the question answers
     * @param value the chosen value of the attribute
     * @param values the chosen values of the attribute
     * @return the answer as saved
     */
    @POST
    @Consumes({
            MediaType.APPLICATION_FORM_URLENCODED,
            })
    @Produces({
            MediaType.APPLICATION_JSON,
            MediaType.APPLICATION_XML,
            MediaType.TEXT_HTML,
            })
    public QuestionAnswer post(@NotNull @FormParam("flowId") UUID flowId,
                               @NotNull @FormParam("orgId") UUID orgId,
                               @NotNull @FormParam("customerId") UUID customerId,
                               @NotNull @FormParam("attributeKey") String attributeKey,
                               @FormParam("value") String value,
                               @FormParam("values") List<String> values);

}

Additional information is available in this old Jersey PR to unify Jersey's date parsing in support of unified JAXB xml/json & BeanParam form objects: https://github.com/jersey/jersey/pull/94

andymc12 commented 6 years ago

We discussed this on the September 12 meeting - here are the minutes for this item:

New issue: 117 - this might be a complicated scenario - might need some sort of “multi-purpose” annotation to indicate that the parameter could be a @BeanParam or an entity…

  • May not make sense if not currently supported on server side yet

I think this is a good idea, but may need to wait until a later release.

Thanks for opening this issue! Andy