Java Reference
In-Depth Information
import
import java.net.URL
java.net.URL ;
@Path ( "/myservice" )
public
public class
class MyService
MyService {
@GET
@Produces ( "text/html" )
public
public String get ( @HeaderParam ( "Referer" ) URL referer ) {
...
}
}
The JAX-RS provider can convert the Referer string header into a java.net.URL because
this class has a constructor that takes only one String parameter.
This automatic conversion also works well when only a valueOf() method exists within the
Java type we want to convert. For instance, let's revisit the @MatrixParam example we used
in this chapter. In that example, we used the @MatrixParam annotation to inject the color of
our vehicle into a parameter of a JAX-RS method. Instead of representing color as a string,
let's define and use a Java enum class:
public
public enum
enum Color {
BLACK ,
BLUE ,
RED ,
WHITE ,
SILVER
}
You cannot allocate Java enums at runtime, but they do have a built-in valueOf() method
that the JAX-RS provider can use:
public
public class
class CarResource
CarResource {
@GET
@Path ( "/{model}/{year}" )
@Produces ( "image/jpeg" )
public
public Jpeg getPicture ( @PathParam ( "make" ) String make ,
@PathParam ( "model" ) String model ,
@MatrixParam ( "color" ) Color color ) {
...
}
JAX-RS has made our lives a bit easier, as we can now work with more concrete Java objects
rather than doing string conversions ourselves.
Search WWH ::




Custom Search