JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to read header parameters in JAX-RS restful web services?


In this page you can see an example to query http header info using @Context annotation and HttpHeaders.

In the previous examples we have given details of application setup, dependencies, web.xml file configurations: If you want to know about these configuration, please refer these:

Restful web services using RESTEasy hello world example.
Restful web services using Jersey hello world example.

package com.java2novice.restful;

import java.util.Set;

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("/http-header")
public class HttpHeaderService {

	@GET
	@Path("query")
	public Response queryHeaderInfo(@Context HttpHeaders httpHeaders){
		
		/** how to get specific header info? **/
		String cacheControl = httpHeaders.getRequestHeader("Cache-Control").get(0);
		System.out.println("Cache-Control: "+cacheControl);
		/** get list of all header parameters from request **/
		Set<String> headerKeys = httpHeaders.getRequestHeaders().keySet();
		for(String header:headerKeys){
			System.out.println(header+":"+httpHeaders.getRequestHeader(header).get(0));
		}
		return Response.status(200).entity("successfully queried header info").build();
	} 
}

Test with "http://localhost:8080/RestfulWebServices/http-header/query" URL, here is the output:

Output:
Cache-Control: max-age=0
[stdout] (http--127.0.0.1-8080-1) host:localhost:8080
[stdout] (http--127.0.0.1-8080-1) connection:keep-alive
[stdout] (http--127.0.0.1-8080-1) cache-control:max-age=0
[stdout] (http--127.0.0.1-8080-1) accept:text/html,application/xhtml+xml,application/xml;q=0.9,
						image/webp,*/*;q=0.8
[stdout] (http--127.0.0.1-8080-1) user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 
						(KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
[stdout] (http--127.0.0.1-8080-1) accept-encoding:gzip,deflate,sdch
[stdout] (http--127.0.0.1-8080-1) accept-language:en-US,en;q=0.8
[stdout] (http--127.0.0.1-8080-1) cookie:__atuvc=24%7C23%2C0%7C24%2C3%7C25%2C45%7C26%2C41%7C27; 
						bsau=14136427273262128776
<< Previous Program | Next Program >>
blog comments powered by Disqus

Restful Web Services Examples

  1. Restful web services using RESTEasy hello world example.
  2. Restful web services using Jersey hello world example.
  3. JAX-RS @Path annotation example
  4. JAX-RS @Path annotation with regular expression match example
  5. JAX-RS @PathParam annotation example
  6. JAX-RS @QueryParam and @DefaultValue annotations example
  7. JAX-RS @MatrixParam annotation example
  8. How to read multiple values of a query paramter in JAX-RS restful web services?
  9. How to pass header parameters as method inputs in JAX-RS restful web services?
  10. How to read header parameters in JAX-RS restful web services?
  11. JAX-RS @FormParam annotation example
  12. How to upload file using Jersey restful web services?
  13. How to download file using java restful web services?
  14. XML based Restful web service with RESTEasy and JAXB.
  15. XML based Restful web service with Jersey and JAXB.
  16. Json based Restful web service with RESTEasy, Jettison and JAXB
  17. Json based Restful web service with RESTEasy and Jackson
  18. Json based Restful web service with Jersey and Jackson
  19. How to input json request with Jersey and Jackson?
  20. Java client for restful web service using java.net package
  21. Java client for restful web service using Jersey API
  22. Java restful webservices with HTTP basic authentication.
Knowledge Centre
Stream and types of Streams
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:

Byte Streams: Provide a convenient means for handling input and output of bytes. Byte Streams classes are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams: Provide a convenient means for handling input & output of characters. Character Streams classes are defined by using two abstract classes, namely Reader and Writer.
Famous Quotations
The greatest obstacle to discovery is not ignorance; it is the illusion of knowledge.
-- Daniel J. Boorstin

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.