3

Let's say I have:

@GET
public UserList fetch(@PathParam("user") String userId) {
    // Do stuff here
}

Now, let's say I have my own type for userId, let's call it UserId. Is it possible to parse that String to UserId when it is passed into the fetch method, i.e.:

@GET
public UserList fetch(@PathParam("user") UserId userId) {
    // Do stuff here
}

I realize I can parse the String once I am inside the method, but it would be more convenient that my method gets the type I want.

zloster
  • 1,111
  • 11
  • 24
Kousha
  • 22,419
  • 30
  • 119
  • 232

2 Answers2

3

Well you've attempted to make a GET call with a request body is what I find not very helpful. Do read Paul's answer here -

you can send a body with GET, and no, it is never useful to do so

What would be good to practice is, to make a PUT or a POST call (PUT vs POST in REST) as follows -

@POST
@Path("/some-path/{some-query-param}")
public Response getDocuments(@ApiParam("user") UserId userId,
                             @PathParam("some-query-param") String queryParam) {
    UserId userIdInstance = userId; // you can use the request body further

Note - The ApiParam annotation used is imported from the com.wordnik.swagger.annotations package. You can similarily use FormParam,QueryParam according to your source of input.

Community
  • 1
  • 1
Naman
  • 23,555
  • 22
  • 173
  • 290
2

Dropwizard is using Jersey for HTTP<->Java POJO marshalling. You could use the various annotations from Jersey @*Param (@FormParam, @QueryParam, etc.) for some of the parameters.

If you need to use map/marshall to/from Java POJOs take a look at the test cases in Dropwizard:

@Path("/valid/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ValidatingResource {
    @POST
    @Path("foo")
    @Valid
    public ValidRepresentation blah(@NotNull @Valid ValidRepresentation representation, @QueryParam("somethingelse") String xer) {
        return new ValidRepresentation();
    }

This defines an API endpoint responding to HTTP POST method which expects ValidRepresentation object and "somethingelse" as HTTP method query parameter. The endpoint WILL respond ONLY when supplied with JSON parameters and will return only JSON objects (@Produces and @Consumes on the class level). The @NotNull requires that object to be mandatory for the call to succeed and @Valid instructs Dropwizard to call Hibernate validator to validate the object before calling the endpoint.

The ValidRepresentation class is here:

package io.dropwizard.jersey.validation;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;

public class ValidRepresentation {
    @NotEmpty
    private String name;

    @JsonProperty
    public String getName() {
        return name;
    }

    @JsonProperty
    public void setName(String name) {
        this.name = name;
    }
}

The POJO is using Jackson annotations to define how JSON representation of this object should look like. @NotEmtpy is annotation from Hibernate validator.

Dropwizard, Jersey and Jackson take care of the details. So for the basic stuff this is all that you need.

zloster
  • 1,111
  • 11
  • 24