[英]How is response converted to JSON in spring boot web application?
I have not included jackson-databind in my pom.我没有在我的 pom.xml 中包含 jackson-databind。 However, when I call an endpoint thatlo returns an object, it is converted to JSON.
但是,当我调用一个返回 object 的端点时,它会转换为 JSON。 If my pom does not include jackson-databind how is the response converted to JSON.
如果我的 pom 不包含 jackson-databind,那么响应如何转换为 JSON。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
@RestController public class TradeController { @RestController 公共 class TradeController {
@RequestMapping("/trade")
public Trade hello() {
String helloWorldMessage = "Hello world from java2blog!";
Trade t = new Trade();
t.setId(1);
return t;
}
} }
Calling http://localhost:8080/trade returns {"id":1}调用 http://localhost:8080/trade 返回 {"id":1}
If you run mvn dependency:tree
, you'll see all of the dependencies that are included by default with the spring-boot-starter-web
dependency.如果您运行
mvn dependency:tree
,您将看到默认包含在spring-boot-starter-web
依赖项中的所有依赖项。 Notice that jackson-databind is included.请注意,其中包含 jackson-databind。
Ex:前任:
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.4.3:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.4.3:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.4:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.4:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.11.4:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.4:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.4:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.4:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.4.3:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.43:compile
[INFO] | | +- org.glassfish:jakarta.el:jar:3.0.3:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.43:compile
[INFO] | +- org.springframework:spring-web:jar:5.3.4:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.3.4:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.3.4:compile
[INFO] | +- org.springframework:spring-aop:jar:5.3.4:compile
[INFO] | \- org.springframework:spring-expression:jar:5.3.4:compile
Behind the scenes, spring is leveraging the jackson module to convert your response to JSON.在幕后,spring 正在利用 jackson 模块将您的响应转换为 JSON。
The Answer is答案是
@RestController
@RequestMapping(value = "mycontroller")
public class MyController {
@RequestMapping(value = "/trade")
@ResponseBody
public ResponseEntity<String> hello() {
JSONObject res = new JSONObject();
String helloWorldMessage = "Hello world from java2blog!";
Trade t = new Trade();
t.setId(1);
res.put("student Object",t);
return new ResponseEntity<>(res.toString(),HttpStatus.OK);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.