RESTful Web service Interview Question

  • What is RESTful Web Services?
  • REST is the acronym for REpresentational State Transfer. REST is an architectural style for developing applications that can be accessed over the network.
    REST is a stateless client-server architecture where web services are resources and can be identified by their URIs. Client applications can use HTTP GET/POST methods to invoke RESTful web services. REST doesn’t specify any specific protocol to use, but in almost all cases it’s used over HTTP/HTTPS. When compared to SOAP web services, these are lightweight and doesn’t follow any standard. We can use XML, JSON, text or any other type of data for request and response.
  • What are advantages of REST web services?
  • What are disadvantages of REST web services?
  • What are different HTTP Methods supported in Restful Web Services?
  • HTTP methods are – GET, POST, PUT, DELETE and HEAD.
  • What is the use of Accept and Content-Type Headers in HTTP Request?
  • Accept headers tells web service what kind of response client is accepting, so if a web service is capable of sending response in XML and JSON format and client sends Accept header as “application/xml” then XML response will be sent. For Accept header “application/json”, server will send the JSON response.
    Content-Type header is used to tell server what is the format of data being sent in the request. If Content-Type header is “application/xml” then server will try to parse it as XML data. This header is useful in HTTP Post and Put requests.
  • How would you choose between SOAP and REST web services?
  • Web Services work on client-server model.
  • What is JAX-RS API?
  • Java API for create RESTful Web Services (JAX-RS) and part of JDK. JAX-RS uses annotations to simplify the development and deployment of web services.
  • Name some implementations of JAX-RS API?
  • What are annotations used in JAX-RS API?
  • How to set different status code in HTTP response?
  • We have to use javax.ws.rs.core.Response class for response.
    		return Response.status(422).entity(exception).build();
    		return Response.ok(response).build(); //200
    	
  • What are the core components of HTTP request and HTTP response?
  • What is the purpose and format of URI in REST architecture?
  • Purpose of URI is to locate resources on the server that are hosting web services.
    		:////
    	
  • How to secure/best practices RESTful web service?
  • How to display custom error pages using RestFull web services ?
  • Need to extend StatusService and implement (Status, Request, Response) method with your custom code now assign instance of your CustomStatusService to appropriate "statusService property".
  • What happens if RestFull resources are accessed by multiple clients ? do you need to make it thread-safe?
  • New Resource instance is created for every incoming Request there is no need to make it thread-safe or add synchronization. Multiple client can safely access RestFull resources concurrently.
  • How to download file in JAX-RS?
  • 		import java.io.File;
    		import javax.ws.rs.GET;
    		import javax.ws.rs.Path;
    		import javax.ws.rs.Produces;
    		import javax.ws.rs.core.Response;
    		import javax.ws.rs.core.Response.ResponseBuilder;
    
    		@Path("/image")
    		public class ImageService {
    
    		private static final String FILE_PATH = "c:\\my.jpg";
    
    			@GET
    			@Path("/get")
    			@Produces("image/jpg")
    			public Response getFile() {
    
    				File file = new File(FILE_PATH);
    
    				ResponseBuilder response = Response.ok((Object) file);
    				response.header("Content-Disposition","attachment; filename=image_from_server.jpg");
    				return response.build();
    			}
    		}
    
    	
  • How to get HTTP request header in JAX-RS?
  • On calling URI: “/persons/get” result: getPerson is called, personAgent : Mozilla/7.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/7.0
    inject directly
    	import javax.ws.rs.GET;
    	import javax.ws.rs.Path;
    	import javax.ws.rs.HeaderParam;
    	import javax.ws.rs.core.Response;
    	
    	@Path("/persons")
    	public class PersonService {
    
    		@GET
    		@Path("/get")
    		public Response getPerson(@HeaderParam("person-agent") String personAgent) {
    
    			return Response.status(200).entity("getPerson is called, personAgent : " + personAgent).build();
    
    		}
    	}
    	
    Another way = Pragmatically via @Context
    	import javax.ws.rs.GET;
    	import javax.ws.rs.Path;
    	import javax.ws.rs.core.Context;
    	import javax.ws.rs.core.HttpHeaders;
    	import javax.ws.rs.core.Response;
    
    	@Path("/persons")
    	public class PersonService {
    
    		@GET
    		@Path("/get")
    		public Response getPerson(@Context HttpHeaders headers) {
    
    		String personAgent = headers.getRequestHeader("person-agent").get(0);
    
    			return Response.status(200).entity("getPerson is called, personAgent : " + personAgent).build();
    
    		}
    	}