...
Exposing an SCA service as a RESTFul endpoint is straightforward – simply use JAX-RS annotations and configure the service with <binding.rs>
in a composite. As a convenience, JAX-RS annotations can be used directly on a component implementation class without the need for a service interface:
Code Block | ||||
---|---|---|---|---|
| ||||
@EndpointUri("messages") @Path("/") @Component @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) @Scope("COMPOSITE") public class MessageService { @PUT @Path("message") public Response create(Message message) \{ // ... } @GET @Path("message/{id}") public Message retrieve(@PathParam("id") Long id) { // ... } @DELETE @Path("message/{id}") public Response delete(@PathParam("id") Long id) { // ... } } |
The EndpointUri
annotation is used to specify the endpoint path to the base container address for a particular transport/protocol. In this case, the transport is HTTP, so the full endpoint address will be <server><port>/messages
.
The above component is can alternatively be configured in a composite as follows:
...
The binding configuration will expose the MessageService resource at <ip address>at <server><port>/messages
.
To consume and produce both XML and JSON, the JAX-RS Consumes and Produces annotations are used:
...
Further examples are provided in the Fabric3 Samples.
Using JAX-RS Providers
Fabric3 supports JAX-RS filter providers. The runtime will recognize JAX-RS Provider
annotations and create a component from the class. This allows filters to be injected with other services as demonstrated below:
Code Block | ||
---|---|---|
| ||
@Provider
@RequireAdmin
@Priority(Priorities.AUTHENTICATION)
@Scope("COMPOSITE")
public class AdminAuthenticationFilter implements ContainerRequestFilter {
@Reference
protected AuthenticationService authenticationService;
public void filter(ContainerRequestContext requestContext) throws IOException {
// ...
}
} |
Provider components should generally be COMPOSITE scope (i.e. multi-threaded).
Exposing Spring Beans as RESTful Resources
...