Skip to main content

Query parameter handling with Spring Boot

In last post, Hello World with Spring Boot we have seen how to create REST service with Spring Boot. Now we will extend the same HelloController to work on query parameters.

Here we will get the "name" as query parameter, and respond with updated message. Update HelloController.java file with below code. 

  
package hello;

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

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String sayHello(@RequestParam(value="name") String name) {
        return  String.format("Hello %s with Spring Boot !!!", name);
    }
}

We have added @RequestParam annotation which will assign value of query parameter 'name' to variable name. Now run you application and hit "http://localhost:8080/hello?name=XYZ" on browser. You should get "Hello XYZ with Spring Boot !!!" in return.
But with this code the "http://localhost:8080/hello" URL will return error "bad request" as query parameter is missing. This is helpful when you want the query parameter as mandatory input.

In order to support optional query parter, you can have default value for parameters as below.
 
package hello;

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

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String sayHello(@RequestParam(value="name", defaultValue="World") String name) {
        return  String.format("Hello %s with Spring Boot !!!", name);
    }
}

'defaultValue' allows the query parameter to be optional. Optional query parameters can be handled using 'required = false' as code below.  'defaultValue' will assign default value to variable 'name', if it's not specified value of 'name' would be null. 


@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String sayHello(@RequestParam(value="name", required = false, defaultValue = "World") String name) {
        return  String.format("Hello %s with Spring Boot !!!", name);
    }
}

Comments

Popular posts from this blog

How to boot Samsung Galaxy SL( GT-I9003) in recovery mode

** I Will Not responsible for any damage done with use of below mentioned methods. ** Power off your Samsung Galaxy SL (GT- I9003) to start in recovery mode. Once you have power off the phone, press and Hold Volume UP and Home button. Make sure you have hold volume up, don't press in  the middle of the volume buttons.  Now keep pressing these two buttons(VOL UP + HOME) and presspower button for 2-3 seconds. Release only power button, but keep pressing Volume Up + Home Button, until you see the screen shown below. Once you see recovery screen release the Home button else your phone will restart as first selected option will be restart and pressing home will make that happen.

In the recovery mode, volume up & down act as a up scroll and down scroll buttons, by pressing Home button you will be able to click the selected option.

How to flash CWM Recovery to Samsung Galaxy SL( GT-I9003) using Odin

** I Will Not responsible for any damage done with use of below mentioned methods. ** To flash the CWM recovery to your Samsung Galaxy SL you will require to following things in place: Odin  : you can download it from here.CWM Recovery image : you can download it from here. Thanks to XDA-developers. USB driver to connect your phone with computer. Install Samsung Kias so it will install the USB driver. Once you have downloaded above required items and installed the USB driver, reboot you phone to download mode. You can read how to reboot phone in download mode from here. Now connect the phone with USB cable to computer. 
Extract the Odin from the zip file that you have downloaded at some location on your computer and launch the Odin executable by double clicking it. Wait for Odin to detect your phone.You will be able to see it in yellow once detected on odin screen as shown below.
Click the PDA button to select the CWM recovery image. Once you have selected the recovery image, check the od…

Exception handling with RestTemplate

Spring framework offers RestTemplate for consuming RESTful services. Recently I used RestTemplate for consuming one of the third party RESTful service.

RestTemplate offers 'ResponseErrorHandler'; which you can extend to decide which are errors for you and how to handle those error. You can override hasError and handleError methods of ResponseErrorHandler.
In handleError you can throw customException if you want, as outlined in below code.

import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; import org.springframework.web.client.ResponseErrorHandler; import java.io.IOException; public class RestTemplateErrorHandler implements ResponseErrorHandler { @Override public void handleError(ClientHttpResponse response) throws IOException { throw new MyCustomException(response.getStatusText(), response.getStatusCode()); } @Override public boolean hasError(ClientHttpResponse response) throws IOException …