diff --git a/spring-web-modules/spring-rest-http-3/pom.xml b/spring-web-modules/spring-rest-http-3/pom.xml index 1f2c66e999..2de635c1f1 100644 --- a/spring-web-modules/spring-rest-http-3/pom.xml +++ b/spring-web-modules/spring-rest-http-3/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter-web + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + \ No newline at end of file diff --git a/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/AppConfig.java b/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/AppConfig.java new file mode 100644 index 0000000000..c9c370e3cb --- /dev/null +++ b/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/AppConfig.java @@ -0,0 +1,13 @@ +package com.baeldung.xml; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AppConfig { + + public static void main(String[] args) { + SpringApplication.run(AppConfig.class, args); + } + +} diff --git a/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/controller/User.java b/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/controller/User.java new file mode 100644 index 0000000000..3b3ba0e91f --- /dev/null +++ b/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/controller/User.java @@ -0,0 +1,79 @@ +package com.baeldung.xml.controller; + +import java.util.Objects; + +public class User { + + private Long id; + private String firstName; + private String secondName; + public User() { + } + + public User(final Long id, final String firstName, final String secondName) { + this.id = id; + this.firstName = firstName; + this.secondName = secondName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(final String firstName) { + this.firstName = firstName; + } + + public String getSecondName() { + return secondName; + } + + public void setSecondName(final String secondName) { + this.secondName = secondName; + } + + public void setId(final Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + final User user = (User) o; + + if (!Objects.equals(id, user.id)) { + return false; + } + if (!Objects.equals(firstName, user.firstName)) { + return false; + } + return Objects.equals(secondName, user.secondName); + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + (firstName != null ? firstName.hashCode() : 0); + result = 31 * result + (secondName != null ? secondName.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", secondName='" + secondName + '\'' + + '}'; + } +} diff --git a/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/controller/UserEchoController.java b/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/controller/UserEchoController.java new file mode 100644 index 0000000000..7c3b99220c --- /dev/null +++ b/spring-web-modules/spring-rest-http-3/src/main/java/com/baeldung/xml/controller/UserEchoController.java @@ -0,0 +1,25 @@ +package com.baeldung.xml.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/users") +public class UserEchoController { + @ResponseStatus(HttpStatus.CREATED) + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public User echoJsonUser(@RequestBody User user) { + return user; + } + + @ResponseStatus(HttpStatus.CREATED) + @PostMapping(consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE) + public User echoXmlUser(@RequestBody User user) { + return user; + } +} diff --git a/spring-web-modules/spring-rest-http-3/src/test/java/com/baeldung/xml/UserEchoControllerIntegrationTest.java b/spring-web-modules/spring-rest-http-3/src/test/java/com/baeldung/xml/UserEchoControllerIntegrationTest.java new file mode 100644 index 0000000000..54e7fa7551 --- /dev/null +++ b/spring-web-modules/spring-rest-http-3/src/test/java/com/baeldung/xml/UserEchoControllerIntegrationTest.java @@ -0,0 +1,71 @@ +package com.baeldung.xml; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.baeldung.xml.controller.User; +import com.baeldung.xml.controller.UserEchoController; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +@WebMvcTest(UserEchoController.class) +class UserEchoControllerIntegrationTest { + + private static final String URI = "/users"; + private static final User USER = new User(1L, "John", "Doe"); + private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); + private static final XmlMapper XML_MAPPER = new XmlMapper(); + + @Autowired + private UserEchoController controller; + + @Autowired + private MockMvc mockMvc; + + @Test + void whenContextStartBeansArePresent() { + assertThat(controller).isNotNull(); + assertThat(mockMvc).isNotNull(); + } + + @Test + void givenEndpointWhenPostJsonUserReceiveCorrectResponse() throws Exception { + final String payload = JSON_MAPPER.writeValueAsString(USER); + MockHttpServletRequestBuilder builder = + MockMvcRequestBuilders.post(URI) + .contentType(MediaType.APPLICATION_JSON) + .content(payload); + + final MvcResult mvcResult = this.mockMvc.perform(builder) + .andExpect(MockMvcResultMatchers.status() + .isCreated()).andReturn(); + final User actual + = JSON_MAPPER.readValue(mvcResult.getResponse().getContentAsString(), User.class); + assertThat(actual).isEqualTo(USER); + } + @Test + void givenEndpointWhenPostXmlUserReceiveCorrectResponse() throws Exception { + final String payload = XML_MAPPER.writeValueAsString(USER); + MockHttpServletRequestBuilder builder = + MockMvcRequestBuilders.post(URI) + .contentType(MediaType.APPLICATION_XML) + .accept(MediaType.APPLICATION_XML) + .content(payload); + + final MvcResult mvcResult = this.mockMvc.perform(builder) + .andExpect(MockMvcResultMatchers.status() + .isCreated()).andReturn(); + final User actual + = XML_MAPPER.readValue(mvcResult.getResponse().getContentAsString(), User.class); + assertThat(actual).isEqualTo(USER); + } + +}