Skip to content

Instantly share code, notes, and snippets.

@martin-mfg
Last active September 29, 2023 07:49
Show Gist options
  • Save martin-mfg/9bae0392ee3a26bac5cc388a6c8b1469 to your computer and use it in GitHub Desktop.
Save martin-mfg/9bae0392ee3a26bac5cc388a6c8b1469 to your computer and use it in GitHub Desktop.
please see 1st comment below
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Validated
@Controller
@SpringBootApplication
public class DemoApp {
public static void main(String[] args) {
SpringApplication.run(DemoApp.class, args);
}
@RequestMapping(
method = RequestMethod.GET,
value = "/todos/1/",
produces = {"text/plain"}
)
public ResponseEntity<String> exampleOperation(
@RequestParam(value = "par1", required = false, defaultValue = "true") Boolean par1,
@RequestParam(value = "par2", required = false, defaultValue = "1.23") Float par2
) {
return new ResponseEntity<>(par1 + " " + par2, HttpStatus.OK);
}
}
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>openapi-spring</artifactId>
<name>openapi-spring</name>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
@martin-mfg
Copy link
Author

martin-mfg commented Sep 28, 2023

Purpose

Demo Spring Boot server which to show that even with defaultValue, parameters can be null.

How to run

  1. move the java file to src/main/java/demo/DemoApp.java
  2. run mvn compile exec:java '-Dexec.mainClass=demo.DemoApp'

Results

request url response
http://localhost:8080/todos/1/?par1=true&par2=3&x=y true 3.0
http://localhost:8080/todos/1/?par1=%20&par2=%20&x=y null null

When using the latest Spring 6.1.0-M5, the 2nd request results in an HTTP status code 500.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment