I have not included jackson-databind in my pom. However, when I call an endpoint thatlo returns an object, it is converted to JSON. If my pom does not include jackson-databind how is the response converted to 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 {
@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}
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. Notice that jackson-databind is included.
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.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.