Skip to content

Produce xml problem #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.0.4.RELEASE</org.springframework-version>
<org.springframework-version>5.1.15.RELEASE</org.springframework-version>
<org.aspectj-version>1.8.1</org.aspectj-version>
</properties>

Expand Down Expand Up @@ -134,7 +134,26 @@
<version>2.5</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.1</version>
</dependency>


<!-- Test -->
<dependency>
<groupId>org.springframework</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.springframework.samples.mvc.test;

import org.springframework.beans.factory.annotation.Autowired;
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;

import javax.validation.Valid;

import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
import static org.springframework.http.MediaType.TEXT_XML_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RequestMapping("/test")
@RestController
public class ResourceController {


@RequestMapping(value = "/javax",
method = POST,
consumes = {APPLICATION_XML_VALUE, TEXT_XML_VALUE},
produces = {APPLICATION_XML_VALUE, TEXT_XML_VALUE})
@ResponseStatus(OK)
public String postJavaxBody(@Valid @RequestBody XMLJavaxRequest xmlRequest) {
StringBuilder sb = new StringBuilder();
if (xmlRequest.getHeader() != null) {
sb.append("javax header is not null\n");
if (xmlRequest.getHeader().getId() != null) {
sb.append("javax header's id is not null\n");
} else {
sb.append("javax header's id is null\n");
}
if (xmlRequest.getHeader().getName() != null) {
sb.append("javax header's name is not null\n");
} else {
sb.append("javax header's name is null\n");
}
} else {
sb.append("javax header is null");
}
return sb.toString();
}

// @Override
@RequestMapping(value = "/fasterxml",
method = POST,
consumes = {APPLICATION_XML_VALUE, TEXT_XML_VALUE},
produces = {APPLICATION_XML_VALUE, TEXT_XML_VALUE})
@ResponseStatus(OK)
public String postJacksonXMLBody(@Valid @RequestBody XMLJacksonRequest xmlRequest) {
StringBuilder sb = new StringBuilder();
if (xmlRequest.getHeader() != null) {
sb.append("jackson xml header is not null\n");
if (xmlRequest.getHeader().getId() != null) {
sb.append("jackson xml header's id is not null\n");
} else {
sb.append("jackson xml header's id is null\n");
}
if (xmlRequest.getHeader().getName() != null) {
sb.append("jackson xml header's name is not null\n");
} else {
sb.append("jackson xml header's name is null\n");
}
} else {
sb.append("jackson xml header is null");
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.springframework.samples.mvc.test;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JacksonXmlRootElement(localName = "Request")
public class XMLJacksonRequest {
@JacksonXmlProperty(localName = "Header")
private Header header = new Header();

@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PUBLIC)
@JacksonXmlRootElement(localName = "Header")
public static class Header {
@JacksonXmlProperty(localName = "Id")
private String id;
@JacksonXmlProperty(localName = "Name")
private String name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.springframework.samples.mvc.test;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@XmlRootElement(name = "Request")
@XmlAccessorType(XmlAccessType.FIELD)
public class XMLJavaxRequest {
@XmlElement(name = "Header")
private Header header = new Header();

@Getter
@Setter
@Builder
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@XmlRootElement(name = "Header")
@XmlAccessorType(XmlAccessType.FIELD)
public static class Header {
@XmlElement(name = "Id")
private String id;
@XmlElement(name = "Name")
private String name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.springframework.samples.mvc.test;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.MediaType;
import org.springframework.samples.mvc.AbstractContextControllerTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.io.IOException;
import java.io.InputStream;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@RunWith(SpringJUnit4ClassRunner.class)
public class ResourceControllerTest extends AbstractContextControllerTests {
private MockMvc mockMvc;

@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
}

@Test
public void postJavax() throws Exception {
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Request>\n" +
" <Header>\n" +
" <Id>TestId</Id>\n" +
" <Name>TestName</Name>\n" +
" </Header>\n" +
"</Request>";

this.mockMvc
.perform(
MockMvcRequestBuilders.post("/test/javax")
.accept(MediaType.APPLICATION_XML_VALUE)
.contentType(MediaType.APPLICATION_XML_VALUE)
.content(content)
)
.andDo(res -> System.out.println("======= " + res.getResponse().getContentAsString()));
}

@Test
public void postFasterxml() throws Exception {
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<Request>\n" +
" <Header>\n" +
" <Id>TestId</Id>\n" +
" <Name>TestName</Name>\n" +
" </Header>\n" +
"</Request>";
this.mockMvc
.perform(
MockMvcRequestBuilders.post("/test/fasterxml")
.accept(MediaType.APPLICATION_XML_VALUE)
.contentType(MediaType.APPLICATION_XML_VALUE)
.content(content)
)
.andDo(res -> System.out.println(">>>>>>> " + res.getResponse().getContentAsString()));
}


private String getFileAsString(String fileName) {
InputStream inputFile = getClass().getResourceAsStream(fileName);
try {
return IOUtils.toString(inputFile)
// this is windows specific - so that CRLF gets converted to LF
.replace("\r\n", "\n");
} catch (IOException e) {
Assert.fail("Failed parsing file: " + fileName + ". Error was: " + e.getMessage());
return "";
}
}
}