I need to create a post request in Springboot and call an API to update records. The mapping JSON layout is a nested one as mentioned below :
{
"Config": {
"Identity": "",
"Region": ""
},
"OPTION_IP_COLL": {
"OPTION_NAME": "",
"OPTION_VALUE": ""
},
"REC_XPFH": {
"UPDATE_CD": "",
"ENTITY_TYPE": "",
"TN_ID": "",
"PR_ID": ""
},
"REC_GRP": {
"REC_PRPR": {
"PR_UPDATE_CD": "",
"PR_ENTITY": "",
"PR_CL_EFT_IND": "",
"PR_EDI_DEST_ID": "",
"PR_RA_DEST_IND": ""
},
"REC_MCBR": {
"BD_ID": "",
"BR_ACCT_NO": "",
"BR_ACCT_NAME": "",
"BR_ACCT_NO_QUA": ""
}
}
}
As this is a nested one, I am bit confused how to define a POJO(it should be single or multiple) for this request and call it in #PostMapping. I am thinking to create one Main POJO and then different classes for other segments and then call the Main one in the RestController class. I am not sure whether this is a right way to do it or is there something else that I could try. Also, I need some assistance to define REC_GRP class as there are nested segments involved.
MainPOJO.class
public class ProviderMappingPOJO {
private Config config;
private OPTIONAL_IP_CALL option_ip_call;
private REC_XPFH rec_xpfh ;
private REC_GRP rec_grp;
//getter and setter method .....
Config.class
private String Identity ;
private String Region;
// getters and setters method...
OPTION_IP_COLL.class
private String UPDATE_CD;
private String ENTITY_TYPE;
private String TN_ID;
private String PR_ID;
// getter and setter methods ...
REC_XPFH.class
// Not sure how to define structure for this as there are nested segments
RestController.class
public class RestController {
#PostMapping(url)
public <> getProviderDetails(#RequestBody MainPOJO main){
// code
}
You can use one main pojo and inside that main pojo, you can define child pojos as you are mentioned above.
public class MainPOJO {
private Config config;
private OPTIONAL_IP_CALL option_ip_call;
private REC_XPFH rec_xpfh ;
private REC_GRP rec_grp;
// setters and getters
}
You can refer the below links, which will helps you to get a clear understanding about the conversion process.
https://dzone.com/articles/converting-json-to-pojos-using-java
http://www.jsonschema2pojo.org/
https://www.freecodeformat.com/json2pojo.php
Once you completed the conversion, you can use the Spring Rest controller to consume the json.
public class RestController {
#PostMapping(path="url", consumes = "application/json")
public <> getProviderDetails(#RequestBody MainPOJO main){
// code
}
}
Related
I have a simple json like this
{
"someReports":[
{
"reportName": "PR123",
"fields": [
]
},
{
"reportName": "PR234",
"fields": []
}
]
}
I have a class that looks somewhat like this inside which getSomeReports() is defined.
class AHeckLotOfReports {
private String someString;
private List<SomethingElse> some;
..
#JsonProperty("someReports")
private List<SomeReports> someReports;
}
//POJO:
class SomeReport {
String reportName;
List<Field> fields;
...
}
//REST Controller looks like this. some injection code is cleaned up
#Api(tags = “SomeReport”)
#Controller
#ThreadSafe
#RequestMapping(“/report/v2")
public class ReportController{
#ApiOperation(value = "Create a new report.”)
#RequestMapping(value = “/report”, method = RequestMethod.POST)
#ResponseBody
public ReportResponse addReport(
#Nonnull final AuthorizationToken authorizationToken,
#Nonnull#RequestBody final AHeckLotOfReports reportRequest) {
final Report report=this.reportService.addReport(
authorizationToken,
reportRequest.getName(),
reportRequest.isEnabled(),
reportRequest.getConfiguration().or(ReportConfiguration.empty()),
reportRequest.getNewConfiguration(),
reportRequest.getDefinition());
return ReportResponse.fromReport(report);
}
I haven't been able to get this working. I do get the structure intact but reportName comes up blank, the fields array comes up empty.
I have tried JsonAlias("someReports","some_reports") and that seems to make no difference.
Anything comes out as an obvious "duh" in this ?
EDIT: My apologies, I did realize I have not provided the entire context. I AM able to deserialize the simple POJO with ObjectMapper. But the class AHeckLotOfReports is used as request object in REST endpoint for a POST and this is where the problem surfaces
Software: jackson.core 2.9.9, jackson.datatype 2.9.8 and JDK 8.0, Spring-Boot 2.2.6
I am consuming the public API for crypto currencies in Mexico: https://api.bitso.com/v3/available_books/ that returns a json like this one:
"success": true,
"payload": [
{
"book": "btc_mxn",
"minimum_price": "500.00",
"maximum_price": "16000000.00",
"minimum_amount": "0.000075",
"maximum_amount": "500.00000000",
"minimum_value": "5",
"maximum_value": "10000000.00"
},
{
"book": "eth_btc",
"minimum_price": "0.00000100",
"maximum_price": "5000.00000000",
"minimum_amount": "0.00000100",
"maximum_amount": "1000.00000000",
"minimum_value": "0.00000100",
"maximum_value": "2000.00000000"
},
and the code that consumes it with Webclient is:
#Override
public Mono<Coins> getCoins() {
return webClient.get().uri("https://api.bitso.com/v3/available_books/")
.accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(Coins.class);
}
The POJOs that are trying to bind it are:
#Data
public class Coins {
#JsonProperty("success")
private String success;
#JsonProperty("playload")
private List<Coin> playload;
and
#Data
public class Coin {
#JsonProperty("book")
private String book;
#JsonProperty("minimum_amount")
private String minimumAmount;
#JsonProperty("maximum_amount")
private String maximumAmount;
#JsonProperty("minimum_price")
private String minimumPrice;
#JsonProperty("maximum_price")
private String maximumPrice;
#JsonProperty("minimum_value")
private String minimumValue;
#JsonProperty("maximum_value")
private String maximumValue;
So far, it only maps like this
"success": true,
"payload": null
You need to have no-args construct and change the word playload to payload :)
I don't think this is a WebFlux issue, but rather a Jackson + Lombok issue.
What happens if you try to deserialize that payload with raw ObjectMapper?
I think Jackson requires an all args constructor annotated with #JsonCreator or ask Lombok to create a #NoArgConstructor for that class. In any case, rewriting your Coin class as a regular Java class should work.
Also, your Coins class has a typo since it's trying to get playload instead of payload.
FIXED: Typo at property name playload instead of payload
Is there a convenient way to convert a Java class with a nested array of Java classes into JSON? For instance I want to convert an instance f the following class to JSON:
public class Students {
private final String serial_no;
private final class InnerData {
private final String[] strs;
private final String name;
private final String city;
}
private final StudentList[] students;
}
as
{
"serial_no" : null,
students : [
{
"strs" : ["athlete", "grammarian"],
"name" : "John Smith",
"city" : "Auckland"
},
{
"strs" : ["postmaster", "swimmer"],
"name" : "Jane Doe",
"city" : "Sydney"
}
]
}
What is the best way to do this in Spring? The examples I have come across seem to be simple classes so far with no nesting.
To return an java object in JSON form from an spring objects requires two simple configurations:
1) Adding 'jackson-mapper-asl' dependency to the classpath
2) Add #ResponseBody annotation to the controller's method
1) Adding 'jackson-mapper-asl' dependency to the classpath
In a spring mvc project we need to add a 'jackson-mapper-asl' dependency to the pom.xml file, and object to json conversion is done by default.
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.10</version> </dependency>
2) Add #ResponseBody annotation to the controller's method
Second thing we need to do is to use '#ResponseBody' annotation against the controller's method. This will make spring understand that method return value should be bound to the web response body.
#RequestMapping("studentlist") public
#ResponseBody List<Student> getStudentList() { List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student(23, "Meghna", "Naidu", "meghna#gmail.com", "8978767878")); studentList.add(new Student(3, "Robert", "Parera", "robert#gmail.com", "8978767878")); studentList.add(new Student(93, "Andrew", "Strauss", "andrew#gmail.com", "8978767878")); studentList.add(new Student(239, "Eddy", "Knight", "knight#gmail.com", "7978767878")); return studentList;
}
I'm trying to use Jersey and Jackson (although any other way of doing JSON demarshalling works) to get this into my system in some form (be it POJO or some other representation).
Basically I only need the data section. I was trying to use GenericTypes with lists, but this is a nested list and I'm just not sure what to do. Lots of kudos for help and I really appreciate it!
{
"total": 4,
"data": [
{
"descriptor": "",
"multiInstance": false,
"active": false
},
{
"descriptor": "Apparel",
"multiInstance": true,
},
{
"descriptor": "abcd123",
"multiInstance": false,
},
{
"descriptor": "abcd",
"multiInstance": false,
}
]
}
This is the class I'm trying to use. I need a list of the class.
public class customObject {
#JsonProperty(value = "descriptor")
private String descriptor;
#JsonProperty(value = "multiInstance")
private Boolean multiInstance;
//getters and setters
}
Edit:
I'm using it in here.
CustomObjectResponse WDCOResponse =
resource
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header("Authorization", getToken()).get(WDCOResponse.class);
But it's still not working.
Edit2:
Figured this out! Thanks to everyone. :)
I had to add annotation to tell it to ignore if something wasn't found, some of the JSON I'm getting back was not fully-formed in that not all fields were absolutely neccesary.
If you the object you provided is what you are passing to your controller, then you will need one more wrapper object to contain the list like this:
public class CustomRequest {
#JSonProperty(value = "total");
private Integer total;
#JsonProperty(value = "data")
private List<CustomObject> data;
// getters/setters
}
public class CustomObject {
#JsonProperty(value = "descriptor")
private String descriptor;
#JsonProperty(value = "multiInstance")
private Boolean multiInstance;
// getters/setters
}
Then your controller will just have annotations that show that the RequestBody is the CustomRequest class:
#Controller
public class JSONController {
#RequestMapping(value="sendData")
public #ResponseBody CustomResponse sendData(
#RequestBody CustomRequest request)
{
return null;
}
}
If you are still getting errors, please provide detailed error or problem. Thanks!
You'd use POJO like:
public class Response {
int count;
List<customObject> data;
}
and access the data from there:
for (customObject ob : response.data) {
// process ig
}
I am currently writing a Java client that consumes a RESTful service, using the Restlet package and its Jackson extension.
I want to query the service for a user and deserialize the response to a User POJO that looks as follows:
#JsonIgnoreProperties(ignoreUnknown=true)
public class User {
private Integer uid;
private String name;
private String mail;
private String field_nickname;
// omitted for brevity: getters/setters, toString
}
A sample response from the service looks as follows:
{
"uid": "5",
"name": "John Doe",
"mail": "john#example.com",
"field_nickname": {
"und": [{
"value": "jdoe",
"format": null,
"safe_value": "jdoe"
}]
}
}
Here is the Java client code:
import org.restlet.resource.ClientResource;
public class TestClient {
public static void main(String[] args) throws Exception {
// Getting a User
ClientResource cr = new ClientResource("http://localhost/rest/user/7.json")
User user = cr.get(User.class);
System.out.println(user);
// Creating a User
cr = new ClientResource("http://localhost/rest/user.json");
User user = new User();
user.setName("Jane Doe");
user.setFieldNick("jdoe2");
user.setMail("jdoe2#example.com");
cr.post(user);
}
The serialization/deserialization of the uid, name and mail fields is very straightforward and poses no problems.
My problem is with field_nickname: The field always contains the array und with a single entry that always looks the same.
How can I tell Jackson to deserialize this field to a String that holds the value of field_nickname[und][0][value] and serialize the attribute into such an array?
This is kind of one-off structural transformation that you will need to write your own handler. The easiest way is probably just to implement something like:
#JsonProperty("field_nickname")
public void setNickname(NicknameWrapper[] wrapper) {
field_nickname = wrapper[0].value;
}
#JsonIgnoreProperties(ignoreUnknown=true)
static class NicknameWrapper {
public String value;
}
and perhaps reverse (getNickname()) as well.