Web Service

1. RESTful API

1.JAX-RS: Java API for RESTful Web Services(JAX-RS) is a Java programming language API spec that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern.

JAX-RS uses annotations, introduced in Java SE 5, to simplify the development and deployment of web service clients and endpoints. From version 1.1 on, JAX-RS is an official part of Java EE 6.

Implementations:

Jersey, Jello-Framework...

Jello is an end-to-end Java application framework optimized for Google App Engine including comprehensive Data Authorization model, a powerful RESTful engine, and out-of-the-box UI views. Jello's REST offers a clean, and simple to follow, JSON format. Its protocol schema follows the OData specification.

2.Json

Spring REST has the Jackson lib to convert the java object to json and json to java object.

Jackson: a high-performance JSON processor for java.

3,

2 Restful VS SOAP

SOAP: simple object access protocol. It defines a standard communication protocol (set of rules) specification for XML -based message exchange. SOAP uses different transport protocols, such as HTTP and SMTP.

REST: representational state transfer. While accessing RESTful resources with HTTP protocol, the URL of the resource serves as the resource identifier and GET, PUT, DELETE, POST and HEAD are the standard HTTP operations to be performed on that resource.

REST:

  • RESTful web services are stateless. You can test this condition by restarting the server and checking if interactions survive.

  • For most servers, RESTful web services provide a good caching infrastructure over an HTTP GET method. This can improve the performance if the information the service returns is not altered frequently and is not dynamic.

  • Service producers and consumers must understand the context and content being passed along as there is no standard set of rules to describe the REST web services interface.

  • REST is useful for restricted-profile devices, such as mobile, for which the overhead of additional parameters are less (e.g., headers).

  • REST services are easy to integrate with existing websites and are exposed with XML so the HTML pages can consume the same with ease. There is little need to refactor the existing site architecture. As such, developers are more productive because they don't need to rewrite everything from scratch; instead, they just need to add on the existing functionality.

  • A REST-based implementation is simple compared to SOAP.

SOAP

  • The Web Services Description Language (WSDL) describes a common set of rules to define the messages, bindings, operations and location of the service. WSDL is akin to a contract to define the interface that the service offers.

  • SOAP requires less plumbing code than REST services design (e.g., transactions, security, coordination, addressing and trust). Most real-world applications are not simple and support complex operations, which require conversational state and contextual information to be maintained. With the SOAP approach, developers don't need to write plumbing code into the application layer.

  • SOAP web services, such as JAX-WS, are useful for asynchronous processing and invocation.

  • SOAP supports several protocols and technologies, including WSDL, XSDs and WS-Addressing

Soap Vs Rest

SOAP is definitely the heavyweight choice for Web service access. It provides the following advantages when compared to REST:

  • Language, platform, and transport independent (REST requires use of HTTP)

  • Works well in distributed enterprise environments (REST assumes direct point-to-point communication)

  • Standardized

  • Provides significant pre-build extensibility in the form of the WS* standards

  • Built-in error handling

  • Automation when used with certain language products

REST is easier to use for the most part and is more flexible. It has the following advantages when compared to SOAP:

  • No expensive tools require to interact with the Web service

  • Smaller learning curve

  • Efficient (SOAP uses XML for all messages, REST can use smaller message formats)

  • Fast (no extensive processing required)

  • Closer to other Web technologies in design philosophy

SOAP vs REST Web Services

There are many differences between SOAP and REST web services. The important 10 differences between SOAP and REST are given below:

No.

SOAP

REST

1)

SOAP is a protocol.

REST is an architectural style.

2)

SOAP stands for Simple Object Access Protocol.

REST stands for REpresentational State Transfer.

3)

SOAP can't use REST because it is a protocol.

REST can use SOAP web services because it is a concept and can use any protocol like HTTP, SOAP.

4)

SOAP uses services interfaces to expose the business logic.

RESTuses URI to expose business logic.

5)

JAX-WS is the java API for SOAP web services.

JAX-RS is the java API for RESTful web services.

6)

SOAP defines standards to be strictly followed.

REST does not define too much standards like SOAP.

7)

SOAP requires more bandwidthand resource than REST.

RESTrequires less bandwidthand resource than SOAP.

8)

SOAP defines its own security.

RESTful web services inherits security measures from the underlying transport.

9)

SOAP permits XML data format only.

REST permits different data format such as Plain text, HTML, XML, JSON etc.

10)

SOAP is less preferred than REST.

REST more preferred than SOAP.

3. @PathParam VS @PathVariable

@PathParam: from Javax.ws.rs eg. using jersey to implement the rest api

@PathVariable: from Spring framework e.g. Spring rest

4.@RequestParam VS @PathVariable

The key difference between@RequestParamand@PathVariableis that @RequestParam used for accessing the values of the query parameters where as @PathVariable used for accessing the values from the URI template.

@RequestMapping(value = "/hello/{id}", method = RequestMethod.GET, headers = "Accept = application/json")
public String getDetails(@PathVariable String id)  {
                ...
}
@RequestParam

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

In the above URL request, the values forparam1andparam2can be accessed as below:

public String getDetails(
    @RequestParam(value="param1", required=true) String param1,
        @RequestParam(value="param2", required=false) String param2){
...
}

5. difference between controller and servlet

6. HTTP response ERROR CODE

200: OK ----shows success

201: CREATED ----when a resource is successfully created using POST or PUT request. Returns link to the newly created resource using the location header.

204: NO CONTENT ---- when response body is empty. For example, a DELETE request.

304:NOT MODIFIED ----- used to reduce network bandwidth usage in case of conditional GET requests. Response body should be empty. Headers should have date, location, etc.

400: BAD REQUEST ----- states that an invalid input is provide. For example, validation error, missing data.

401: UNAUTHORIZED ----- states that user is using invalid or wrong authentication token.

403: FORBIDDEN-------states that the user is not having access to the method being used. For example, delete access without admin rights.

404:NOT FOUND ----- states that the method is not available.

409: CONFLICT ------ states conflict situation while executing the method. For example, adding duplicate entry.

500: INTERNAL SERVER ERROR ------ states that the server has thrown some exception while executing the method.

7 HTTP request method

HTTP Request

Description

GET

Asks to get the resource at the requested URL.

POST

Asks the server to accept the body info attached. It is like GET request with extra info sent with the request.

HEAD

Asks for only the header part of whatever a GET would return. Just like GET but with no body.

TRACE

Asks for the loopback of the request message, for testing or troubleshooting.

PUT

Says to put the enclosed info (the body) at the requested URL.

DELETE

Says to delete the resource at the requested URL.

OPTIONS

Asks for a list of the HTTP methods to which the thing at the request URL can respond

1

GETThe GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

2

HEADSame as GET, but it transfers the status line and the header section only.

3

POSTA POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

4

PUTReplaces all the current representations of the target resource with the uploaded content.

5

DELETERemoves all the current representations of the target resource given by URI.

Last updated