Skip to main content

Developing Restful-API using JAX-RS

Web Services 

  • Services that are exposed to the Internet for programmatic access. 
  • Two main types of web services
    • REST (Representational State Transfer)
      • REST has no standard protocol (can use XML,JSON,etc)
    • SOAP
  • Web services use HTTP (Hyper Text Transfer Protocol) to send and receive messages.
  • To include service definitions that offered from Web Service: WSDL

URI

  • Two main types of URI
    • Resource Based URI
      • Every resource must be identified by a single URI 
      • Doesn't contain .do in URI ( /message/{message_Id})
    • Collection URI
      • return collection of instances
      • ex : /message/1/comments -> return all the comments relevant to message id=1
      • To create new resource you have to use collection URI

Filtering  

  • /messages?offset=30&limit=10 -> return 10 messages starting from message_Id=30

HTTP Methods 

  • Four main types of HTTP methods:
    • Get: To get the details of resource
    • Put: To update resources
    • Post: To create new resource
    • Delete: To delete the resource
  • Other HTTP methods: head, options
  • HTTP methods can be in two types
    • Idempotent: safely repeatable (put, get, delete)
    • Non-idempotent: can not safely repeatable (post)

Status Codes

  • Five main types of status code that indicate by first number of three digits
    • 1xx: Informational codes (acknowledgement/response)
    • 2xx: Success codes
    • 3xx: Redirection codes (ask clients do something)
    • 4xx: Client Error Codes
    • 5xx: Server errors

Hypermedia As The Engine Of Application State(HATEOAS)

  • Use to provide links to resources
  • rel: Describe the relationship to a current resource (What that link point to)

API Levels 

  • Three main types of API levels
    • Level1: Use resource based URI
    • Level2: Use HTTP methods
    • Level3: Use HATEOAS

Additional Information 

  • Hypertext: Text that contains links to other texts (hyperlinks)
  • Static web pages : Every resource has URI and specific HTML file
  • Jersey: Library that handles all the API functionalities in the deploying server (ex: tomcat)
  • Maven: Project management utility
    • Archetype: template (previously created projects)
    • Group id: Package id
    • Artifact Id: project name 

Implementing Messenger App Using Jax-RS

  • Create Maven project 
    • select archetype as Jersey-webapp
  • Crete tomcat server
  • Create resources
    • @Path(/messages): To choose class
    • @GET: Tell the relevant HTTP method
    • @produces(MediaType.TEXT_PLAIN): Tell the return type
    • @XmlRootElement: convert types to XML type
  • Create database class using hashmap
  • Include a variable in a path parameter
    • @Path("/{message_id}") : use curly braces
    • get this one as a variable to the method: use @PathParam
  • To get JSON type: uncomment dependencies in pom.xml file
  • For post requests
    • @consumes(MediaType.TEXT_PLAIN) : Tell the posting data type
  • Send Parameters
    • @QueryParam("year") : /messages?year=2015
      • Query param use? 
    • @MatrixParam("year"): /message;year=2015
      • Matrix param use;
    • @HeaderParam("year")
      • To get header values by their name
    • @CookieParam("year")
      • To get cookie values
    • @Context UriInfo uriinfo: To get all the information if you are not sure about values
      • To get all the necessary parameters (path, URL, query, cookies etc)
    • @BeanParam : @BeanParam BeanParam(class name) beanparam
      • Save other parameters in a class and access them using getters and setters
  • @XmlTransient: ignore the below data when you expose other data 
  • To change headers: use Response.created()
  • To create URI : use uriinfo.getAbsolutePathBuilder()
  • To handle the exception: use WebApplicationException

Comments

Popular posts from this blog

Java- Interview

Here are some popular tips in Java General Question JVM - Java Virtual Machine. Used to execute byte code. Platform dependent (but byte code is platform independent) JRE- Java Run time Environment. It provides run time environment and it's the implementation of JVM. It contains libraries and files that need to execute JVM.   JDK- Java Development Kit. It contains JRE and development tools. Why java doesn't support pointers: It's insecure and complex to understand We can save java file with another name if class is not defined as public Java use camalCase notation for objects naming (ex: firstName) OOP Concepts Related  Aggregation: weak relationship (bike has an indicator) Composition:  strong relationship (bike has an engine) GUI Java GUI - Install Window builder to create GUI using Eclipse IDE Code Related No issues when you write static public void instead of public static void We can execute program without main method : using static bloc...

Core Java Concepts

Java Applications  There are mainly four types of Java applications. Standalone Web  Enterprise  Mobile Java Features  There are several features in Java, but in here listed some important ones Platform independent Object oriented Multi threaded Distributed Robust (automatic garbage collection, exception handling, type checking) Java Security  Java has 3 built-in security components; Classloader: Separate class files packages from imported packages. BytecodeVerifier: check the illegal access to objects Security Manager: check what resources can access the object. C++ vs Java Methods Static - No need to create an object to invoke the static method. Constructor - use to initialize objects Constructor name must same as the class name Constructor has no return type There are two types of constructors Default - no parameter Parameterized If there is no constructor defined in the class, compiler aut...

JavaScript- Scopes and Closures

Scope The scope is the set of variables, objects, and functions you have access to. Create Scope  Use functions Declaring Variables  If you do write operation without declaring variable -> work fine foo = 10; console.log(foo);  -> 10 If you do read operation without declaring variable -> doesn't work fine console.log(foo) -> syntax error Window Object Inbuilt object provided by JavaScript itself Every global variable and functions are part of window object JavaScript is both compilations and interpreted language Compilation: register variables, method names, arguments in global scope Interpretation: assign values to global scope and execute the code global scope problem: undeclared function inside the variable it goes to global scope Hoisting It doesn't matter where you declare your variable (In the compilation step it include all variables automatically before the interpretation) Strict Mode To get stri...