Showing posts with label HttpServletRequest. Show all posts
Showing posts with label HttpServletRequest. Show all posts

Wednesday, 11 March 2020

Spring: HttpServletRequest: Get url with query parameters

Below snippet return the url with query parameters.
private String getUrlWithQueryParms(final HttpServletRequest request) {
 if(request.getQueryString() == null) {
  return request.getRequestURL().toString();
 }
 return request.getRequestURL().append("?").append(request.getQueryString()).toString();
}

Find the complete working application.

HomeController.java
package com.sample.app.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

 private String getUrlWithQueryParms(final HttpServletRequest request) {
  if(request.getQueryString() == null) {
   return request.getRequestURL().toString();
  }
  return request.getRequestURL().append("?").append(request.getQueryString()).toString();
 }

 @GetMapping("/")
 public String home(HttpServletRequest request) {

  String result = getUrlWithQueryParms(request);
  
  return result;
 }

}

App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
 
 public static void main(String[] args) {

  SpringApplication.run(App.class, args);

 }
}

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.sample.app</groupId>
 <artifactId>springrestURI</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>

 <name>springbootApp</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 </dependencies>
</project>

Total project structure looks like below.


Run App.java.

Open below url in browser.
http://localhost:8080/?name=krishna&age=31

You will get the same url in response.

You can download complete working application from this link.



Previous                                                    Next                                                    Home

Wednesday, 7 August 2019

Spring boot REST: Get a reference to HttpServletRequest

You can get access to HttpServletRequest in controller method by simply passing HttpServletRequest as an argument to controller method.

Example
@RequestMapping("/headers")
public String headers(HttpServletRequest request) {

    ......
    ......

}

Find the below working application.

App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {

        SpringApplication.run(App.class, args);

    }
}

HomeController.java
package com.sample.app.cotroller;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String home() {

        return "Hello World";
    }

    @RequestMapping("/headers")
    public String headers(HttpServletRequest request) {

        Enumeration<String> headerNames = request.getHeaderNames();

        StringBuilder builder = new StringBuilder();

        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);

            builder.append(headerName).append(" : ").append(headerValue).append("<br />");

        }

        return builder.toString();
    }

}


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>springRest</groupId>
    <artifactId>springRest</artifactId>
    <version>1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <name>springbootApp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>


Total project structure looks like below.

Run App.java and open the url 'http://localhost:8080/headers', you can see all the request headers.    

You can download complete working application from this link.

Previous                                                    Next                                                    Home