<scope>provided</scope>
in maven . For applications in servlet containers, there are several implementations: package com.example; import javax.ws.rs.*; @Path("/") public interface RestService { @GET @Path("echo") String echo(@QueryParam("q") String original); }
package com.example; import javax.enterprise.ApplicationScoped; import javax.ws.rs.*; @ApplicationScoped @Path("/") public class Rest implements RestService{ @GET @Path("echo") @Override public String echo(@QueryParam("q") String original) { return original; } }
@Path
, for example, indicates the relative or absolute path to this resource; annotation group @GET
, @POST
, @PUT
, @DELETE
is responsible for the type of HTTP
request relevant to this method.@Produces
and @Consumes
are used to indicate the MIME type of the content of the result / data. Accordingly, the data class must be annotated by JAXB . Standard providers in JBoss AS 7 are resteasy-jaxb-provider (xml-marshaller / anmarschaller) and resteasy-jettison-provider (json). These two providers allow you to integrate with a large number of external services, provide XML and JSON API to the outside.javax.ws.rs.core.Response
: @GET @Path("file/get/{name}") Response getFile(@PathParam("file") String fileName) { if(!Files.exists(Paths.get(fileName)) { return Response.status(422).entity("I'm a teapot"); } else { Response.Builder response = Response.ok(); response.header("X-Some-Server-Header", "value"); response.entity(new StreamingOutput() { @Override public void write(OutputStream outputStream) throws IOException, WebApplicationException { Files.copy(Paths.get(fileName), outputStream); } }); return response.build(); } }
ClientRequest
, ClientResponse<T>
and ending with the generation of proxy objects over an annotated JAX-RS interface. For example, using the RestService
interface from the first example: RestService service = ProxyFactory.create(RestService.class, "http://localhost:8080/example"); log.info(service.echo("test message"));
ClientRequest
/ ClientResponse<T>
can be used, using apache httpcomponents (in Resteasy 2.3.x.GA) or apache-httpclient (in 2.2.x.GA). The usage example is as follows: ClientRequest request = new ClientRequest(url); request.header("X-Additional-Header", "header value"); // : GET, POST, PUT, DELETE ClientResponse<String> response = request.get(String.class); if(response.getCode() == 200) { String result = response.getEntity(); log.info(result); }
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
Source: https://habr.com/ru/post/140181/
All Articles