Skip to content
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

shardingsphere-jdbc-5.5.0 causes spring boot to return JSON data as XML #31203

Open
Comven opened this issue May 11, 2024 · 7 comments
Open

shardingsphere-jdbc-5.5.0 causes spring boot to return JSON data as XML #31203

Comven opened this issue May 11, 2024 · 7 comments

Comments

@Comven
Copy link

Comven commented May 11, 2024

spring-boot: 2.7.18、3.2.4
shardingsphere-jdbc:5.5.0

The introduction of the 'jackson-dataformat-xml' component dependency in version 5.5 resulted in the JSON returned by the spring boot program being changed to XML format.

shardingsphere-jdbc-core-5.4.1 Return format is

[
    {
        "id": 1,
        "name": "ZhangSan",
        "address": "BeiJing",
        "addTime": "2024-05-11 10:05:10"
    }
]

shardingsphere-jdbc-5.5.0 Return format is

<List>
<item>
<id>1</id>
<name>ZhangSan</name>
<address>BeiJing</address>
<addTime>2024-05-11 09:48:56</addTime>
</item>
</List>
@linghengqian
Copy link
Member

@Comven
Copy link
Author

Comven commented May 11, 2024

Can shardingsphere-jdbc be made to not rely on the jackson-dataformat-xml component package, otherwise using the shardingsphere-jdbc-.5.5.0, the spring boot program will not be able to return JSON properly? Originally, our business program did not introduce this problematic jackson-dataformat-xml component package because shardingsphere jdbc automatically introduced this package, which caused this problem. @linghengqian

@linghengqian
Copy link
Member

  • @Comven Shardingsphere always needs a library for parsing xml files. In the past it was JAXB API, now it is jackson-dataformat-xml, and it can also be xstream in the future. The reason has been explained clearly in Removes use of JAXB API via jackson-dataformat-xml #29384 . Using jackson-dataformat-xml will allow shardingsphere to better protect against breaking changes in Spring Boot 3.x and GraalVM Native Image.
  • You must provide unit tests to prove why just introducing jackson-dataformat-xml will cause WebMVC or Webflux in Spring Boot to actually change the return value of the Controller. There have been 4 issues so far, and no one has provided a minimal unit test.
  • Prove it. I believe this is 100% a problem with Spring Boot or a third-party Spring Boot Stater, because there is no reason to change the configuration of Spring Boot by just introducing jackson-dataformat-xml, which is just an xml parsing library.

@linghengqian
Copy link
Member

@lazeyliu @Comven

With the expected xml dependency

	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'

the following controller:

@RestController
public class JsonXmlController {
	@GetMapping("/book")
	public Book book() {
		return new Book("Intro to Spring Boot", "Spring team");
	}
	public record Book(String title, String author) {
	}
}

I'm getting the following behavior:

http localhost:8080/book -v
GET /book HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/3.2.1



HTTP/1.1 200
Connection: keep-alive
Content-Type: application/json
Date: Wed, 05 Oct 2022 19:14:27 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "author": "Spring team",
    "title": "Intro to Spring Boot"
}
  • I have every reason to believe that you should provide unit tests first, and unit tests are probably completely unrelated to shardingsphere.

@Comven
Copy link
Author

Comven commented May 24, 2024

@lazeyliu @Comven

With the expected xml dependency

	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'

the following controller:

@RestController
public class JsonXmlController {
	@GetMapping("/book")
	public Book book() {
		return new Book("Intro to Spring Boot", "Spring team");
	}
	public record Book(String title, String author) {
	}
}

I'm getting the following behavior:

http localhost:8080/book -v
GET /book HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/3.2.1



HTTP/1.1 200
Connection: keep-alive
Content-Type: application/json
Date: Wed, 05 Oct 2022 19:14:27 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked

{
    "author": "Spring team",
    "title": "Intro to Spring Boot"
}
  • I have every reason to believe that you should provide unit tests first, and unit tests are probably completely unrelated to shardingsphere.

I have said earlier that this bug is due to jackson-dataformat-xml, but shardingsphere-5.5.0 uses the functionality of this component to cause such problems in programs that introduce shardingsphere-jdbc, which is relevant. That's why I said can you switch back to jaxb parsing xml instead of jackson-dataformat-xml, otherwise jackson-dataformat-xml has always had this problem without changing, Our projects using shardingsphere-jdbc-5.5.0 need to deal specifically with the issue of the response data being returned in xml。

The problem of replicating jackson-dataformat-xml is relatively easy。
spring-boot-2.7.18 or later,Introduce the following since coordinates

<dependency>
    <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>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>

Write a piece of logic that returns JSON content, as shown in the following code
image

Accessing this interface address directly in a browser yields the following results
image

@linghengqian
Copy link
Member

  • @Comven So, this is actually an issue with Spring Framework's WebMVC. Even if jackson-dataformat-xml is not used and instead, xstream or another XML parsing library is utilized, the problem would still arise if one were to implement an AbstractGenericHttpMessageConverter akin to MappingJackson2XmlHttpMessageConverter. I lean towards this being an issue that should be raised at https://github.com/spring-projects/spring-framework/issues , specifically that merely introducing jackson-dataformat-xml should not activate the MappingJackson2XmlHttpMessageConverter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants