BAEL-7009: Using XML in @RequestBody in Spring REST (#15472)
* BAEL-7009: Using XML in @RequestBody in Spring REST * BAEL-7009: Fix parametrization issue * BAEL-7009: Added JAXB dependencies * BAEL-7009: Back to Java 8 and javax * Revert "BAEL-7009: Back to Java 8 and javax" This reverts commit e5f55f344dab75d434d143083196bd00181a3518. * BAEL-7009: Migrated the solution to Jackson XML * BAEL-7009: Remove REST file * BAEL-7009: Cleanup
This commit is contained in:
parent
be84fd1b62
commit
f0c4180670
|
@ -20,6 +20,10 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue