{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"topic": "SsS",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
Now I'm going to create relevent java classes. This is very easy when use jsonschema2pojo .
Paste above JSON String space of jsonschema2pojo space.
(Follow bellow image)
You can preview All java classes using click priview button.You can downlord it clicking zip button and classname-source.zip link.
I created Spring Boot test Project
Package Name - com.example.demo
(Because aboue jsonschema2pojo I used com.example.demo.pojo)
Now I can copy downloaded json classes to the com.example.demo package.
Follow package structure
Add jackson dependencies to the pom.xml file
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Create WelcomController classpackage com.example.demo.pojo; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class WelcomeController { @RequestMapping(value = "/jsontopojo", method = RequestMethod.POST ) public @ResponseBody Example value(@RequestBody Example example){ return example; } }
Now run the project and use ARC or Postman client to request.
url - http://localhost:8080/jsontopojo
method -POST
body - Add to above json string
If You need to return some string of request json body,
package com.example.demo.pojo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomeController {
@RequestMapping(value = "/jsontopojo", method = RequestMethod.POST )
public @ResponseBody String value(@RequestBody Example example){
return example.getGlossary().getTitle();
}
}
You can see return "example glossary".
Again Simple Example without using jsonschema2pojo site.
Follow bellow json string
Again Simple Example without using jsonschema2pojo site.
Follow bellow json string
{
"car": {
"title": "example glossary",
"bmw": {
"title": "S",
"topic": "SsS"
}
}
}
Create Main class
package com.example.demo.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class JsontoPojo {
@JsonProperty("car")
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
Create Car class
package com.example.demo.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Car {
@JsonProperty("title")
private String title;
@JsonProperty("bmw")
private BMW bmw ;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public BMW getBmw() {
return bmw;
}
public void setBmw(BMW bmw) {
this.bmw = bmw;
}
}
Create BMW class
package com.example.demo.pojo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class BMW {
@JsonProperty("title")
private String title;
@JsonProperty("topic")
private String topic;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
}
package com.example.demo.pojo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomeController {
@RequestMapping(value="/car" , method = RequestMethod.POST)
public @ResponseBody JsontoPojo type(@RequestBody JsontoPojo jsontoPojo){
return jsontoPojo;
}
}
URL - http://localhost:8080/car
Thank You
Others
No comments:
Post a Comment