http://www.developerscrappad.com/2308/java/java-ee/rest-jax-rs/java-rest-jax-rs-2-0-how-to-handle-date-time-and-timestamp-data-types/
https://jersey.java.net/documentation/1.19/client-api.html
Related: jersey-2x
https://jersey.java.net/documentation/1.19/client-api.html
Related: jersey-2x
Client
instances are expensive resources. It is recommended a configured instance is reused for the creation of Web resources. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client
instance and WebResource
instances may be shared between multiple threads.
In the above cases a
WebResource
instance will utilize HttpUrlConnection
or HttpsUrlConnection
, if the URI scheme of the WebResource
is “http” or “https” respectively.
While there is no explicit concurrency documentation for
ClientConfig
, it's clear from its source code that its safe to use in a multithreaded environment. The Client
class is also thread safe, leaving just the WebResource
to consider. Based on its documentation I would dedicate a new WebResource
to each thread, meaning your code should look more like this:ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
public void getUser() {
WebResource service = client.resource(RESOURCE_URL);
// Get response as String
String response = service
.path("/addUser")
.accept(MediaType.TEXT_PLAIN)
.get(String.class);
return response;
}
To add any additional mime-type support to the JAX-RS service a few things need to be done:
- The @Produces annotation on the endpoint needs to mention the mime-type supported;
- There must be a MessageBodyWriter implementation registered with the system that is capable of producing the mime-type. In annotation words it means that we need to have a class annotated with @Provider and@Produces that mentions our target mime-type. The implementation needs to marshal a given object to the OutputStream provided.
There are a number of libraries out there to address this: Jackson Extension CSV (jackson-dataformat-csv), Super CSV (with the Dozer plugin for some more complex conversions), Open CSV, Apache Commons CSV and others.
Below is a MessageBodyWriter implementation that supports marshalling of the Object class into CSV or ‘XLS’. XLS is actually just a CSV flavor tailored for Excel so that Excel does not remove tailing zeros for numbers and preserves the original cell text better in general.
http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/
In JAX-RS, for excel file, annotate the method with
@Produces("application/vnd.ms-excel")
:- Put @Produces(“application/vnd.ms-excel”) on service method.
- Set “Content-Disposition” in Response header to prompt a download box.
@GET
@Path("/get")
@Produces("application/vnd.ms-excel")
public Response getFile() {
File file = new File(FILE_PATH);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename=new-excel-file.xls");
return response.build();
}
CSV
http://stackoverflow.com/questions/5161466/how-do-i-use-the-jersey-json-pojo-support
- Add jackson*.jar to your classpath (As stated by @Vitali Bichov);
- In web.xml, if you're using
com.sun.jersey.config.property.packages
init parameter, addorg.codehaus.jackson.jaxrs
to the list. This will include JSON providers in the scan list of Jersey
http://blog.alutam.com/2012/06/26/writing-web-applications-that-can-run-with-jersey-1-x-as-well-as-jersey-2-0/
https://bharatonjava.wordpress.com/2012/09/08/jersey-hello-world-example/
// extend PackagesResourceConfig to get the package scanning functionality
public
class
ExampleResourceConfig
extends
PackagesResourceConfig {
public
ExampleResourceConfig() {
// pass name of the package to be scanned
super
(
"com.example"
);
// add InfoResource as an explicit resource bound to "info" path
this
.getExplicitRootResources().put(
"info"
, InfoResource.
class
);
}
https://bharatonjava.wordpress.com/2012/09/08/jersey-hello-world-example/