RequestBody handling a JSON containing Strings and a List - java

I'm sending a post request from the client to the server. The body of the post request looks like this:
...
body: JSON.stringify
({
command: 'someString',
dataFields: setDataList()
})
...
while the "setDataList()" returns the following structure:
[ {…}, {…}, {…}, ..., {…} ]
[0: {type: "_header_", label: "upload"}
1: {type: "_image_", name: "data:image/jpeg;base64", value: "base64 encoded string", label: "someImage.JPG"}
2: {...}]
I'm having issues processing the content of the "dataFields" key at the server. Currently, the SpringBoot applicaiton looks like this:
#PostMapping(
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE})
public void postBody(#RequestBody ManageRequest manageRequest) {
...
}
and "ManageRequest" like this:
import java.util.List;
public class ManageRequest {
private String accountId;
private String command;
private String transactionId;
private List<String> dataFields;
public String getAccountId() {
return accountId;
}
public String getTransactionId() {
return transactionId;
}
public void setTransactionId(String transactionId) {
this.transactionId = transactionId;
}
public void setAccountId(String accountId) {
this.accountId = accountId;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public List<String> getDataFieldList() {
return dataFields;
}
public void setDataFieldList(List<String> dataList) {
this.dataFields = dataList;
}
}
I'm not experienced in handling such requests with Java. My goal would be to extract the content of the "dataFields" like this: "dataFields[0], dataFields[1], ..." and allocate them to a new List to add to a post request.
At the moment, the list seems to be empty when arriving at the end point.

Your problems is with the List.
The server is waiting for a List of Strings but you are sending a List of objects with fields that are Strings.
private List<String> dataFields; // This property is a List of string not a list of "dataFields"
You should create a class DataField and it would looks like this
public class DataField{
private String type;
private String label;
private String name;
.
.
.
}
And then in your ManageRequest class you should do like this:
public class ManageRequest {
private String accountId;
private String command;
private String transactionId;
private List<DataField> dataFields;
//Getter and setters
}

Related

I'm recieving "Error while extracting response for type" when I try to return the JSON as an Object. How can I solve this?

I'm doing an API that consumes an external API (https://swapi.dev/), but I'm receving this error when I try to return the JSON converted to Object.
I'm trying to solve this for almost 12 hours without any success x_x
Error while extracting response for type [class [Lcom.starwarsapi.filmsapi.model.FilmModel;] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `[Lcom.starwarsapi.filmsapi.model.FilmModel;` from Object value (token `JsonToken.START_OBJECT`);
FilmController:
#RestController
#RequestMapping("/films")
public class FilmController {
#Autowired
private FilmService filmService;
#GetMapping
public List<FilmModel> getAllFilms() {
List<FilmModel> response = filmService.getAllFilms();
return response;
}
FilmModel:
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class FilmModel {
private FilmResultModel[] results;
}
FilmResultModel
#Data
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class FilmResultModel {
private String title;
#JsonProperty("episode_id")
private Integer episodeId;
#JsonProperty("opening_crawl")
private String description;
private String director;
private String producer;
#JsonProperty("release_date")
private String releaseData;
private String[] characters;
private String[] planets;
private String[] starships;
private String[] vehicles;
private String[] species;
private String created;
private String edited;
private String url;
FilmService:
public interface FilmService {
public List<FilmModel> getAllFilms();
}
FilmServiceImpl:
#Service
public class FilmServiceImpl implements FilmService {
#Value("${external.api.url}")
private String filmBaseUrl;
#Autowired
private RestTemplate restTemplate;
#Override
public List<FilmModel> getAllFilms() {
FilmModel[] result = restTemplate.getForObject(filmBaseUrl, FilmModel[].class);
List<FilmModel> films = Arrays.asList(result);
System.out.println(films);
return films;
}
PS¹: external.api.url = https://swapi.dev/api/films/?format=json
PS²: When I return getAllFilms as String, the program works:
#Override
public String getAllFilms() {
String result = restTemplate.getForObject(filmBaseUrl, String.class);
System.out.println(result);
return result;
}
But I need it to return as an object because later I'll try to create a PUT method to change the description of the movie.
try using FilmModel result = restTemplate.getForObject(filmBaseUrl, FilmModel.class);
FilmModel is just the outer wrapper object and contains the list of films.

I have DTO for each json payload. I get the payload through http post request. I get two payloads but how to combine them to final payload

I have DTO for payload1, payload2 and payload3.
How to get the payload3?
using payload 1 and payload 2.
How to establish the mapping between the data models?
DTO 1
public class DataRequestModel {
private DataValue data; //some example
private long timestamp;
#Data
public static class DataValue {
private Value1 value1;
private Value2 value2;
}
#Data
public static class Value1 {
private long val;
}
#Data
public static class Value2 {
private long val;
}
}
Data Model 2
public class Guid{
private string guid;
}
Datamodel 3
public class payload{
private String guid;
private long longvalue;
private long timestamp;
}
## Controller
#RestController
#RequestMapping("/users")
public class DataTransferController {
List<Payload> payload;
#PostMapping("/list")
public void data(#RequestBody DataRequestModel dataRequestModel){
Not sure how to do from here...
for(Payload temp: payload){
temp.getLongValue()=dataRequestModel.getData().
}
}
}
These are the structure of the JSON
1.JSON payload1
{
"data":{
"value1":{
"val":30
}, "value2":{
"val":20
}
},
"timestamp":155501523
}
2.JSON payload2
{
"guid":[
"/value1",
"/value2"
]
}
3.JSON final payload (to be sent)
[
{
"guid":"/value1",
"longvalue":30,
"timestamp":155501523
},
{
"guid":"/value2",
"longvalue":20,
"timestamp":155501523
}
]
My code for generating payload 3 is
#RestController #RequestMapping("/users")
public class DataTransferController
{
List<Payload> payload; #PostMapping("/list")
public void data(#RequestBody DataRequestModel dataRequestModel)
{
Not sure how to do from here... for(TimeseriesPayload temp: timeseriesPayload)
{
temp.getLongValue()=dataRequestModel.getData().
}
}
}
You don't need for loop to do this, in the public void data create List<Payload>
public void data(#RequestBody DataRequestModel dataRequestModel) {
List<Payload> list = new ArrayList<>();
// first object
Payload payload1 = new Payload();
payload1.setGuid(//set value);
payload1.setLongValue(dataRequestModel.getData().getValue1().getVal());
payload1.setTimestamp(dataRequestModel.getTimestamp());
list.add(payload1);
// second object
Payload payload2 = new Payload();
payload12setGuid(//set value);
payload2.setLongValue(dataRequestModel.getData().getValue2().getVal());
payload1.setTimestamp(dataRequestModel.getTimestamp());
list.add(payload2);
And then finally return List

How to get around HTTP Error 415 - Unsupported Media Type

I'm using Restet and I wanted to know if its possible if a ServerResource entity type is set, for example for this type of entity:
#XStreamAlias("role")
#ApiModel
public class Role {
private String entityId;
private String name;
#ApiModelProperty(required = false, value = "")
private List<String> aclRead;
#ApiModelProperty(required = false, value = "")
private List<String> acLWrite;
#ApiModelProperty(required = false, value = "")
private Boolean publicRead;
#ApiModelProperty(required = false, value = "")
private Boolean publicWrite;
public String getEntityId() {
return entityId;
}
public void setEntityId(String entityId) {
this.entityId = entityId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getAclRead() {
return aclRead;
}
public void setAclRead(List<String> aclRead) {
this.aclRead = aclRead;
}
public List<String> getAcLWrite() {
return acLWrite;
}
public void setAcLWrite(List<String> acLWrite) {
this.acLWrite = acLWrite;
}
public Boolean getPublicRead() {
return publicRead;
}
public void setPublicRead(Boolean publicRead) {
this.publicRead = publicRead;
}
public Boolean getPublicWrite() {
return publicWrite;
}
public void setPublicWrite(Boolean publicWrite) {
this.publicWrite = publicWrite;
}
}
How can we be able to POST without the need of passing all the fields, for this example we only wanted to pass the "name" from the Client Request, but doing that throws 415 - Unsupported Media Type
In our client we only do pass this JSON
{
"role" : {
"name" : "AdminRole"
}
}
For the Get response of the Resource, the return type is Role also so the client will get all the fields, entityId, name, aclRead, aclWrite, publicRead and publicWrite through the Restlet marshalling.
The problem we have is that we cannot POST.
In the post request, the json data should only consist of the the entity parameters without the entity name as root key. So, your post request should look like this:
{
"name" : "AdminRole"
}

Unable to parse json with List elements inside it

I have a Java class which has 2 List Object inside it and i am Json serializing the parent class.
#JsonSerialize
public class RequestSalesJson {
#JsonProperty("nonUniqueSalesList")
private List<SalesDataJson> getNonUniqueSalesDataJson;
#JsonProperty("uniqueSalesList")
private List<SalesDataJson> uniqueSalesDataJson;
public List<SalesDataJson> getGetNonUniqueSalesDataJson() {
return getNonUniqueSalesDataJson;
}
public void setGetNonUniqueSalesDataJson(List<SalesDataJson> getNonUniqueSalesDataJson) {
this.getNonUniqueSalesDataJson = getNonUniqueSalesDataJson;
}
public List<SalesDataJson> getUniqueSalesDataJson() {
return uniqueSalesDataJson;
}
public void setUniqueSalesDataJson(List<SalesDataJson> uniqueSalesDataJson) {
this.uniqueSalesDataJson = uniqueSalesDataJson;
}
}
SalesReturnJson.java
#JsonSerialize
public class SalesReturnJson {
#JsonProperty("starttime")
private String startTime;
#JsonProperty("pn")
private String partNumber;
#JsonProperty("so")
private String SalesOrderNumber;
#JsonProperty("wo")
private String workOrderNumber;
#JsonProperty("loc")
//other variables declared..
}
Controller.java :-
#RequestMapping(value = "/addAllSalesData",method = RequestMethod.POST)
public void addAllSalesData(#RequestBody RequestSalesJson requestSalesJsons){
log.info("POST : '/addSalesData'");
try{
System.out.print("In Controller "+requestSalesJsons.getUniqueSalesDataJson());
//salesService.processSalesData(requestSalesJsons);
}
catch(Exception e){
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
}
}
The value here is coming to be null.
Below is the json i am using :-
{ "uniqueSalesJson": [{"SO":4000955,"Part Number":"000","Locator":255638,"Lot Number":"P01-2059139","Reservation Quantity":2,"Status":"Released to warehouse","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"},{"SO":4000955,"Part Number":"000","Locator":255652,"Lot Number":"P01-2059140","Reservation Quantity":10,"Status":"Released to warehouse","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"}],"nonUniqueSalesJson":[{"SO":4000992,"Part Number":"1276M84G15","Locator":12345,"Lot Number":"P01-2344141","Reservation Quantity":6,"Status":"PACKED","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"},{"SO":4000992,"Part Number":"1276M84G15","Locator":12345,"Lot Number":"P01-2344141","Reservation Quantity":6,"Status":"PICKED","COE":"Fabrication","ORG":"P07","Start_Time":"2017-09-19 11:21:36"}]}
There are some issues in your code that let me doubt that your application compiles. First of all, rename the SalesReturnJson class to SalesDataJson.
Then check your #JsonProperty annotations. The value here must match exactly the property key in the Json String. Refactoring all this stuff will lead you to your root entity class:
#JsonSerialize
public class RequestSalesJson {
#JsonProperty("nonUniqueSalesJson")
private List<SalesDataJson> nonUniqueSalesDataJson;
#JsonProperty("uniqueSalesJson")
private List<SalesDataJson> uniqueSalesDataJson;
...
}
and your SalesDataJson class (missing a lot of attributes which the mapper ignores by configuration):
#JsonSerialize
public class SalesDataJson {
#JsonProperty("Start_Time")
private String startTime;
#JsonProperty("Part Number")
private String partNumber;
#JsonProperty("SO")
private String SalesOrderNumber;
}
This sample works as expected with the com.fasterxml.jackson.databind.ObjectMapper
Hope that helps!

Mapping JSON into POJO using Gson

I have the following JSON to represent the server response for a salt request:
{
"USER":
{
"E_MAIL":"email",
"SALT":"salt"
},
"CODE":"010"
}
And i tried to map it with the following POJO:
public class SaltPOJO {
private String code = null;
private User user = null;
#Override
public String toString() {
return this.user.toString();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public class User {
private String e_mail = null;
private String salt = null;
#Override
public String toString() {
return this.e_mail + ": " + this.salt;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
}
}
Now everytime i do this:
Gson gson = new Gson();
SaltPOJO saltPojo = gson.fromJson(json.toString(), SaltPOJO.class);
Log.v("Bla", saltPojo.toString());
The saltPojo.toString() is null. How can i map my JSON into POJO using Gson?
Is the order of my variables important for the Gson mapping?
Is the order of my variables important for the Gson mapping?
No, that's not the case.
How can i map my JSON into POJO using Gson?
It's Case Sensitive and the keys in JSON string should be same as variable names used in POJO class.
You can use #SerializedName annotation to use any variable name as your like.
Sample code:
class SaltPOJO {
#SerializedName("CODE")
private String code = null;
#SerializedName("USER")
private User user = null;
...
class User {
#SerializedName("E_MAIL")
private String e_mail = null;
#SerializedName("SALT")
private String salt = null;
You don't have proper mapping between your getter and setter. If you change your json to something like below, it would work:
{
"user":
{
"email":"email",
"salt":"salt"
},
"code":"010"
}
If you are getting json form third party then unfortunately, you would have to change your pojo or you could use adapter.

Categories