Merge branch 'eugenp:master' into master

This commit is contained in:
Bhabani Prasad Patel 2021-05-22 17:46:18 +05:30
commit 846147ad7f
24 changed files with 288 additions and 46 deletions

View File

@ -21,7 +21,12 @@ public class ErrorResponseInterceptor implements Interceptor {
Gson gson = new Gson();
String body = gson.toJson(new ErrorMessage(response.code(), "The response from the server was not OK"));
ResponseBody responseBody = ResponseBody.create(body, APPLICATION_JSON);
ResponseBody originalBody = response.body();
if (originalBody != null) {
originalBody.close();
}
return response.newBuilder()
.body(responseBody)
.build();

View File

@ -0,0 +1,22 @@
package com.baeldung.mapper;
import com.baeldung.dto.SimpleSource;
import com.baeldung.entity.SimpleDestination;
import com.baeldung.service.SimpleService;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.beans.factory.annotation.Autowired;
@Mapper(componentModel = "spring")
public abstract class SimpleDestinationMapperUsingInjectedService {
@Autowired
protected SimpleService simpleService;
@Mapping(target = "name", expression = "java(simpleService.enrichName(source.getName()))")
public abstract SimpleDestination sourceToDestination(SimpleSource source);
public abstract SimpleSource destinationToSource(SimpleDestination destination);
}

View File

@ -0,0 +1,11 @@
package com.baeldung.service;
import org.springframework.stereotype.Service;
@Service
public class SimpleService {
public String enrichName(String name) {
return "-:: " + name + " ::-";
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.mapper;
import com.baeldung.dto.SimpleSource;
import com.baeldung.entity.SimpleDestination;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SimpleDestinationMapperUsingInjectedIntegrationTest {
@Autowired
private SimpleDestinationMapperUsingInjectedService mapper;
@Test
public void givenSourceToDestination_whenMaps_thenNameEnriched() {
// Given
SimpleSource source = new SimpleSource();
source.setName("Bob");
source.setDescription("The Builder");
// When
SimpleDestination destination = mapper.sourceToDestination(source);
// Then
assertThat(destination).isNotNull();
assertThat(destination.getName()).isEqualTo("-:: Bob ::-");
assertThat(destination.getDescription()).isEqualTo("The Builder");
}
}

View File

@ -70,7 +70,6 @@
<module>spring-boot-swagger-jwt</module>
<module>spring-boot-testing</module>
<module>spring-boot-vue</module>
<module>spring-boot-xml</module>
<module>spring-boot-actuator</module>
<module>spring-boot-data-2</module>
<module>spring-boot-react</module>

View File

@ -5,3 +5,4 @@ This module contains articles about Spring Boot customization 2
### Relevant Articles:
- [DispatcherServlet and web.xml in Spring Boot](https://www.baeldung.com/spring-boot-dispatcherservlet-web-xml)
- [XML Defined Beans in Spring Boot](https://www.baeldung.com/spring-boot-xml-beans)

View File

@ -23,7 +23,10 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

View File

@ -0,0 +1 @@
sample=string loaded from properties!

View File

@ -1,5 +1,6 @@
package com.baeldung.springbootxml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,3 +0,0 @@
### Relevant Articles:
- [XML Defined Beans in Spring Boot](https://www.baeldung.com/spring-boot-xml-beans)

View File

@ -1,39 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>spring-boot-xml</artifactId>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -1 +0,0 @@
sample=string loaded from properties!

View File

@ -24,6 +24,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- runtime and test scoped -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -0,0 +1,16 @@
package com.baeldung.multipartupload;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Employee {
private String name;
private MultipartFile document;
}

View File

@ -0,0 +1,49 @@
package com.baeldung.multipartupload;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@Controller
@AllArgsConstructor
public class EmployeeController {
private final EmployeeService employeeService;
@GetMapping(value = "/employee")
public String showEmployeeForm(Model model) {
model.addAttribute("employee", new Employee());
return "employee/createEmployeeForm";
}
@RequestMapping(path = "/employee", method = POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
public String saveEmployee(@ModelAttribute Employee employee) {
employeeService.save(employee);
return "employee/success";
}
@RequestMapping(path = "/requestpart/employee", method = POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
public ResponseEntity<Object> saveEmployee(@RequestPart Employee employee, @RequestPart MultipartFile document) {
employee.setDocument(document);
employeeService.save(employee);
return ResponseEntity.ok().build();
}
@RequestMapping(path = "/requestparam/employee", method = POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE })
public ResponseEntity<Object> saveEmployee(@RequestParam String name, @RequestPart MultipartFile document) {
Employee employee = new Employee(name, document);
employeeService.save(employee);
return ResponseEntity.ok().build();
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.multipartupload;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository {
void saveEmployee(Employee employee);
}

View File

@ -0,0 +1,36 @@
package com.baeldung.multipartupload;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
@Service
public class EmployeeService {
public void save(Employee employee) {
saveFile(employee.getDocument());
// save other employee data
}
private void saveFile(MultipartFile multipartFile) {
try {
saveToFilesystem(multipartFile);
} catch (Exception e) {
throw new RuntimeException("Unable to save file", e);
}
}
private static void saveToFilesystem(MultipartFile multipartFile) throws IOException {
String dir = Files.createTempDirectory("tmpDir").toFile().getAbsolutePath();
File file = new File(dir + File.pathSeparator + multipartFile.getName());
try (OutputStream os = new FileOutputStream(file)) {
os.write(multipartFile.getBytes());
}
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.multipartupload;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MultipartUploadApplication {
public static void main(String[] args) {
SpringApplication.run(MultipartUploadApplication.class, args);
}
}

View File

@ -0,0 +1,16 @@
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/employee}" th:object="${employee}" method="post" enctype="multipart/form-data">
<p>name: <input type="text" th:field="*{name}" /></p>
<p>document:<input type="file" th:field="*{document}" multiple="multiple"/>
<input type="submit" value="upload" />
<input type="reset" value="Reset" /></p>
</form>
</body>
</html>

View File

@ -0,0 +1,8 @@
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
</head>
<body>
Employee data submitted.
</body>
</html>

View File

@ -0,0 +1,56 @@
package com.baeldung.multipartupload;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.apache.http.entity.ContentType.DEFAULT_BINARY;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
@EnableWebMvc
public class EmployeeControllerIntegrationTest {
private static final MockMultipartFile A_FILE = new MockMultipartFile("document", null, DEFAULT_BINARY.toString(), "Employee Record".getBytes());
@Autowired
private MockMvc mockMvc;
@MockBean
private EmployeeService employeeService;
@Test
public void givenFormData_whenPost_thenReturns200OK() throws Exception {
mockMvc.perform(multipart("/employee")
.file(A_FILE)
.param("name", "testname"))
.andExpect(status().isOk());
}
@Test
public void givenEmployeeJsonAndMultipartFile_whenPostWithRequestPart_thenReturnsOK() throws Exception {
MockMultipartFile employeeJson = new MockMultipartFile("employee", null,
"application/json", "{\"name\": \"Emp Name\"}".getBytes());
mockMvc.perform(multipart("/requestpart/employee")
.file(A_FILE)
.file(employeeJson))
.andExpect(status().isOk());
}
@Test
public void givenRequestPartAndRequestParam_whenPost_thenReturns200OK() throws Exception {
mockMvc.perform(multipart("/requestparam/employee")
.file(A_FILE)
.param("name", "testname"))
.andExpect(status().isOk());
}
}