Thursday, March 24, 2016

Jersey 1.x Tutorial



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
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") :
  1. Put @Produces(“application/vnd.ms-excel”) on service method.
  2. 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

  1. Add jackson*.jar to your classpath (As stated by @Vitali Bichov);
  2. In web.xml, if you're using com.sun.jersey.config.property.packages init parameter, add org.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/
// 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/
      ResourceConfig rc = new PackagesResourceConfig(

                "com.bharat.examples.rest");


Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts