ตัวอย่างการเขียน Spring-boot Reactive Form Data
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
<configuration>
<additionalProperties>
<java.version>${java.version}</java.version>
</additionalProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
หมายเหตุ lombox เป็น annotation code generator ตัวนึง
@SpringBootApplication
public class AppStarter {
public static void main(String[] args) {
SpringApplication.run(AppStarter.class, args);
}
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RegisterForm {
private String firstName;
private String lastName;
private String email;
}
@Slf4j
@RestController
public class RegisterFormController {
@GetMapping({"", "/"})
public Mono<String> hello() {
return Mono.just("Hello world.");
}
@PostMapping(value = "/register1", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public Mono<RegisterForm> register1(final RegisterForm form) {
return Mono.just(form);
}
@PostMapping(value = "/register2", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public Mono<RegisterForm> register2(final ServerWebExchange exchange) {
return exchange.getFormData()
.map(formData -> {
return RegisterForm.builder()
.firstName(formData.getFirst("firstName"))
.lastName(formData.getFirst("lastName"))
.email(formData.getFirst("email"))
.build();
});
}
@PostMapping(value = "/register3", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public Mono<RegisterForm> register3(final RegisterForm form) {
return Mono.just(form);
}
}
cd ไปที่ root ของ project จากนั้น
$ mvn clean package
$ mvn spring-boot:run
เปิด browser แล้วเข้า http://localhost:8080
ลองยิง postman POST ไปยัง
/register1
(Content-Type : application/x-www-form-urlencoded)
/register2
(Content-Type : application/x-www-form-urlencoded)
/register3
(Content-Type : multipart/form-data; boundary=...)