Last active
August 29, 2015 14:01
-
-
Save michiakig/5a3d83dbe00877da5a2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld.core; | |
public enum Color { | |
BLUE, | |
GREEN | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld.params; | |
import com.example.helloworld.core.Color; | |
import com.yammer.dropwizard.jersey.params.AbstractParam; | |
import javax.ws.rs.WebApplicationException; | |
import java.util.Arrays; | |
public class ColorParam extends AbstractParam<Color> { | |
private static final String error = "Possible values are " + Arrays.toString(Color.values()); | |
public ColorParam(String input) throws WebApplicationException { | |
super(input); | |
System.out.println("ColorParam.ctor("+input+")"); | |
} | |
@Override | |
protected Color parse(String raw) { | |
System.out.println("ColorParam.parse("+raw+")"); | |
try { | |
return Color.valueOf(raw); | |
} catch(Exception e) { | |
throw new RuntimeException(error); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.helloworld.resources; | |
import com.example.helloworld.core.Color; | |
import com.example.helloworld.params.ColorParam; | |
import com.google.common.base.Optional; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
@Path("/color") | |
@Produces(MediaType.TEXT_PLAIN) | |
public class ColorResource { | |
Logger log = LoggerFactory.getLogger(ColorResource.class); | |
/** | |
* This is the normal, kind of naive way of doing things without Jersey params, and it has extremely goofy behavior: | |
* If the argument is ok, color will be the parsed enum. if the param is missing, it will be null (of course) | |
* BUT if the argument can't be parsed as a Color, the client will get an HTTP 404 which is just flat out wrong | |
* basically probably never do this | |
*/ | |
@GET | |
@Path("/no_default_enum") | |
public Response noDefault(@QueryParam("color") Color color) { | |
log.info("color={}", color); | |
return Response.ok().build(); | |
} | |
/** | |
* This is an appropriate use of Optional: colorParam will either be Optional.absent() or Optional.of(Color) | |
* It will never be null (of course) | |
*/ | |
@GET | |
@Path("/optional/no_default") | |
public Response optNoDefault(@QueryParam("color") Optional<Color> colorParam) { | |
log.info("colorParam={}", colorParam); | |
return Response.ok().build(); | |
} | |
/** | |
* This is sort of nonsensical: colorParam will never be Optional.absent() | |
* It will never be null (of course) | |
*/ | |
@GET | |
@Path("/optional/with_default") | |
public Response optWithDefault(@QueryParam("color") @DefaultValue("BLUE") Optional<Color> colorParam) { | |
log.info("colorParam={}", colorParam); | |
return Response.ok().build(); | |
} | |
/** | |
* This is the vanilla use of Jersey params: colorParam will either be null (of course) or a parsed ColorParam, | |
* or if the param has a bad value (i.e. can't be parsed as a Color) the client will get HTTP 400 with a nice error | |
* message in the body ("Invalid parameter: RED (Possible values are [BLUE, GREEN])") | |
*/ | |
@GET | |
@Path("/no_default_param") | |
public Response noDefaultParam(@QueryParam("color") ColorParam colorParam) { | |
log.info("colorParam={}", colorParam); | |
return Response.ok().build(); | |
} | |
/** | |
* This behaves as you'd expect, same as above but colorParam will not be null and instead will be the default if it's missing | |
*/ | |
@GET | |
@Path("/default_param") | |
public Response defaultParam(@QueryParam("color") @DefaultValue("BLUE") ColorParam colorParam) { | |
log.info("colorParam={}", colorParam); | |
return Response.ok().build(); | |
} | |
/** | |
* This also behaves as you'd expect, same as above but colorParam will not be null and instead will be Optional.absent if it's missing | |
*/ | |
@GET | |
@Path("/optional/param") | |
public Response optionalParam(@QueryParam("color") Optional<ColorParam> colorParam) { | |
log.info("colorParam={}", colorParam); | |
return Response.ok().build(); | |
} | |
/** | |
* Primitives will be initialized to their "default" values | |
* i.e. flag below will be false if missing | |
*/ | |
@POST | |
@Path("/primitive") | |
public Response primitive(@FormParam("flag") boolean flag) { | |
log.info("flag={}", flag); | |
return Response.ok().build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://codahale.com/what-makes-jersey-interesting-parameter-classes/