BAEL-5054 make sure all files are added
BAEL-5054 organise packages BAEL-5054 move project to correct folder BAEL-5044 remove old folder
This commit is contained in:
@@ -133,7 +133,7 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.Spring5Application</mainClass>
|
||||
<mainClass>com.baeldung.reactive.Application</mainClass>
|
||||
<layout>JAR</layout>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.reactive;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.reactive.controller;
|
||||
|
||||
|
||||
import com.baeldung.reactive.service.ReactiveUploadService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
|
||||
@RestController
|
||||
public class UploadController {
|
||||
final ReactiveUploadService uploadService;
|
||||
|
||||
public UploadController(ReactiveUploadService uploadService) {
|
||||
this.uploadService = uploadService;
|
||||
}
|
||||
|
||||
@PostMapping(path = "/upload")
|
||||
@ResponseBody
|
||||
public Mono<HttpStatus> uploadPdf(@RequestParam("file") final MultipartFile multipartFile) {
|
||||
return uploadService.uploadPdf(multipartFile.getResource());
|
||||
}
|
||||
|
||||
@PostMapping(path = "/upload/multipart")
|
||||
@ResponseBody
|
||||
public Mono<HttpStatus> uploadMultipart(@RequestParam("file") final MultipartFile multipartFile) {
|
||||
return uploadService.uploadMultipart(multipartFile);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fake upload endpoint returning "OK" HttpStatus
|
||||
* @return "OK" HttpStatus
|
||||
*/
|
||||
@PostMapping(path = "/external/upload")
|
||||
@ResponseBody
|
||||
public HttpStatus externalUpload() {
|
||||
return HttpStatus.OK;
|
||||
}
|
||||
|
||||
@GetMapping("/trixi")
|
||||
public String returnTrixi() {
|
||||
return "Trixi";
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.reactive.service;
|
||||
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.MultipartBodyBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
@Service
|
||||
public class ReactiveUploadService {
|
||||
|
||||
private final WebClient webClient;
|
||||
private static final String EXTERNAL_UPLOAD_URL = "http://localhost:8080/external/upload";
|
||||
|
||||
public ReactiveUploadService() {
|
||||
this.webClient = WebClient.create();
|
||||
}
|
||||
|
||||
|
||||
public Mono<HttpStatus> uploadPdf(final Resource resource){
|
||||
|
||||
final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri();
|
||||
Mono<HttpStatus> httpStatusMono = webClient.post()
|
||||
.uri(url)
|
||||
.contentType(MediaType.APPLICATION_PDF)
|
||||
.body(BodyInserters.fromResource(resource))
|
||||
.exchangeToMono(response -> {
|
||||
if (response.statusCode().equals(HttpStatus.OK)) {
|
||||
return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
|
||||
} else {
|
||||
System.out.println("Failed to upload pdf. " + response.statusCode());
|
||||
}
|
||||
return null;
|
||||
});
|
||||
return httpStatusMono;
|
||||
}
|
||||
|
||||
|
||||
public Mono<HttpStatus> uploadMultipart(final MultipartFile multipartFile){
|
||||
final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri();
|
||||
|
||||
final MultipartBodyBuilder builder = new MultipartBodyBuilder();
|
||||
builder.part("file", multipartFile.getResource());
|
||||
|
||||
Mono<HttpStatus> httpStatusMono = webClient.post()
|
||||
.uri(url)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(BodyInserters.fromMultipartData(builder.build()))
|
||||
.exchangeToMono(response -> {
|
||||
if (response.statusCode().equals(HttpStatus.OK)) {
|
||||
return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
|
||||
} else {
|
||||
System.out.println("Failed to upload pdf. " + response.statusCode());
|
||||
}
|
||||
return null;
|
||||
});
|
||||
return httpStatusMono;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
logging.level.root=INFO
|
||||
|
||||
server.port=8081
|
||||
server.port=8080
|
||||
|
||||
logging.level.reactor.netty.http.client.HttpClient=DEBUG
|
||||
Reference in New Issue
Block a user