HttpRequestHandlingMessagingGateway JSON array payload - java

I can't get an HTTP inbound adapter to convert a JSON array to a list of objects of type SendgridTxEvent, it always ends up with ArrayList<LinkedHashMap> instead of List<SendgridTxEvent>. The config:
public HttpRequestHandlingMessagingGateway sendgridMessageAdapter(#Qualifier("sendgridWebhookEvents") MessageChannel channel) {
HttpRequestHandlingMessagingGateway httpInboundChannelAdapter = new HttpRequestHandlingMessagingGateway(false);
RequestMapping mapping = new RequestMapping();
mapping.setMethods(HttpMethod.POST);
mapping.setPathPatterns("/webhook/sendgrid");
ParameterizedTypeReference<List<SendgridTxEvent>> ptr = new ParameterizedTypeReference<List<SendgridTxEvent>>() {
};
httpInboundChannelAdapter.setRequestMapping(mapping);
httpInboundChannelAdapter.setRequestChannel(channel);
httpInboundChannelAdapter.setRequestPayloadType(ResolvableType.forType(ptr));
return httpInboundChannelAdapter;
}
If I set the request payload type to httpInboundChannelAdapter.setRequestPayloadType(ResolvableType.forType(SendgridTxEvent.class)) and feed a JSON object to it (instead of an array), jackson deserializes SendgridTxEvent correctly, so the problem only occurs with an array input. Input examples can be found here.
How do I go about consuming JSON arrays in an HTTP inbound adapter?
SendgridTxEvent class:
#JsonIgnoreProperties(ignoreUnknown = true)
public class SendgridTxEvent {
public enum Event {
PROCESSED,
DROPPED,
DELIVERED,
BOUNCE,
DEFERRED,
OPEN,
CLICK,
UNSUBSCRIBE,
SPAMREPORT;
#JsonCreator
public static Event forValue(String value) {
return Event.valueOf(value.toUpperCase());
}
}
private String email;
private Long timestamp;
private Event event;
#JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List<String> category;
private String sgEventId;
private String sgMessageId;
//getters, setters
}

I've raised an issue to fix this in the Framework: https://github.com/spring-projects/spring-integration/issues/2806
Meanwhile as a workaround I would suggest to expect a payload in the HttpRequestHandlingMessagingGateway as String or byte[], then use a POJO #Transformer downstream with direct conversion via ObjectMapper and already your expected <List<SendgridTxEvent> type.
Another simple option that you can expect just SendgridTxEvent[].class and than convert it into the list downstream.

Related

Convert object to string Spring Boot rest API

I'm receiving an id (integer) and a executor (String) in my controller (Rest API). However, when looking at my database, I see that the string is being inserted into the database as an object. Example of database entry:
{
   "executor": "Pietje"
}
Controller:
#PostMapping(path = "/accept/{id}")
public String acceptAssignment(#Valid #PathVariable Integer id, #RequestBody String executor) {
return assignmentService.acceptAssignment(id, executor);
}
Service implementation:
#Override
public String acceptAssignment(Integer id, String executor) {
Assignment assignment = assignmentRespository.findById(id).orElse(null);
assignment.setExecutor(String.valueOf(executor));
AssignmentDTO assignmentDTO = assignmentConverter.convertEntityToDto(assignment);
assignmentRespository.save(assignment);
return assignmentDTO.getExecutor();
}
What am I doing wrong, and how can I fix it?
I could pass along the entire DTO instead of just the 'executor' value, but that doesn't seem efficient. As far as I know, the problem is not with the frontend but I could add the React code if necessary.
TL;DR - you're using a String containing a JSON-object as if it was an attribute of this JSON-object. The solution it to treat this JSON properly.
Note that you don't need to mess with desirialization manually, let the JSON-converter of the framework do its job.
All that you need is a simple POJO:
#Getter
#Setter
public class AssignmentExecutor {
private String executor;
}
The above POJO can be automatically translated in & to the following of JSON without any effort from your side (owing to the magic of Spring):
{
"executor": "Pietje"
}
It would be automatically parsed to the proper type by a Spring's message-converter, you only need to specify that you need an AssignmentExecutor instead of a plain String.
#PostMapping(path = "/accept/{id}")
public String acceptAssignment(#Valid #PathVariable Integer id,
#RequestBody AssignmentExecutor executor) {
return assignmentService.acceptAssignment(id, executor);
}
Note
Introducing this new type would not require any changes in the Assignment, executor can still be represented as a String field.
By invoking orElse(null) on the optional result, you're creating a potential problem by depriving the possibility to get a meaningful exception if the data that corresponds to the given id was not found. In such a case, your current code would trigger a NullPointerException right on the next line. Instead, I would advise providing a suitable exception via Optional.orElseThrow().
A now again all that you need is to return an instance of AssignmentExecutor and it would be automatically converted into JSON:
#Override
public String acceptAssignment(Integer id, AssignmentExecutor executor) {
Assignment assignment = assignmentRespository.findById(id)
.orElseThrow(() -> new MyException("Assignment with id " + id + " was not found"));
assignment.setExecutor(executor.getExecutor());
assignmentRespository.save(assignment);
return executor;
}
You can use ObjectMapper to fetch the desired key-value from the #RequestBody String executor as:
Approach Here:
The #RequestBodythat you are getting from the API is in the object form and using ObjectMapper , it will be converted into a Map of attributes as in the API request and we can fetch the desired key-value pair.
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = mapper.readValue(executor, new TypeReference<>() {});
String s = (String) map.get("executor");
Added in acceptAssignment method as:
#Override
public String acceptAssignment(Integer id, String executor) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> requestMap = mapper.readValue(executor, new TypeReference<>() {});
Assignment assignment = assignmentRespository.findById(id).orElse(null);
assignment.setExecutor((String)requestMap.get("executor"));
AssignmentDTO assignmentDTO = assignmentConverter.convertEntityToDto(assignment);
assignmentRespository.save(assignment);
return assignmentDTO.getExecutor();
}

Converting a JSON with array of objects to proper Java object

I have a simple Spring Boot project in which a scheduler periodically consumes a RESTful API and converts the incoming JSON file.
The JSON file is actually an array of Objects with some Keys and Values:
[
{"CoID":1,"CoName":"کشاورزی و دامپروری مگسال","CoNameEnglish":"MagsalAgriculture & Animal Husbandry Co.","CompanySymbol":"MAGS","CoTSESymbol":"زمگسا","GroupID":1,"GroupName":"كشاورزی و دامپروری","IndustryID":1,"IndustryName":"كشاورزی، دامپروری و خدمات وابسته به آن","InstCode":"5054819322815158","TseCIsinCode":"IRO1MAGS0006","TseSIsinCode":"IRO1MAGS0001","MarketID":1,"MarketName":"بورس"},
{"CoID":2,"CoName":"ذغالسنگ نگین طبس","CoNameEnglish":"Negin Tabas Lignite Co.","CompanySymbol":"TBAS","CoTSESymbol":"کطبس","GroupID":2,"GroupName":"استخراج و انبار ذغال سنگ سخت","IndustryID":2,"IndustryName":"استخراج ذغال سنگ","InstCode":"8977369674477111","TseCIsinCode":"IRO1TBAS0004","TseSIsinCode":"IRO1TBAS0001","MarketID":1,"MarketName":"بورس"},{"CoID":3,"CoName":"معدنی و صنعتی چادرملو","CoNameEnglish":"Chadormalu Mining & Industrial Co.","CompanySymbol":"CHML","CoTSESymbol":"کچاد","GroupID":3,"GroupName":"استخراج سنگ معدن های فلزی آهنی","IndustryID":3,"IndustryName":"استخراج كانه های فلزی","InstCode":"18027801615184692","TseCIsinCode":"IRO1CHML0000","TseSIsinCode":"IRO1CHML0001","MarketID":1,"MarketName":"بورس"}
...
]
I have a class called Company with similar fields to one of objects in the array within the JSON file:
#JsonIgnoreProperties(ignoreUnknown = true)
public class Company {
private int CoID;
private String CoName;
private String CoNameEnglish;
private String CompanySymbl;
private String CoTSESymbl;
private int GroupID;
private String GroupName;
private int IndustryID;
private String IndustryName;
private String IndustryCode;
private String TseCIsinCode;
private String TseSIsinCode;
private int MarketID;
private String MarketName;
// And proper getters, setters and constructor //
I also created a wrapping class called CompanyList:
public class CompanyList {
private ArrayList<Company> companyList;
public ArrayList<Company> getCompanyList() {
return companyList;
}
public void setCompanyList(ArrayList<Company> companyList) {
this.companyList = companyList;
}
public CompanyList() {
}
#Override
public String toString() {
return "CompanyList [companyList=" + companyList + "]";
}
}
I have tried three different ways to fulfill this requirement:
First:
Object[] forNow = restTemplate.getForObject("somewhere", Object[].class);
List<Object> cp= Arrays.asList(forNow);
This one works properly.
Second:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<Company>> response = restTemplate.exchange(
"somewhere",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Company>>(){});
List<Company> companies = response.getBody();
log.info(companies.toString());
This one is compiled successfully but returns null and 0 in all fields.
Third:
CompanyList cp = restTemplate.getForObject("somewhere", CompanyList.class);
log.info(cp.getCompanyList().toString());
This one raises an exception:
Error while extracting response for type [class ir.pisys.rest.CompanyList] and content type [application/json;charset=utf-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of ir.pisys.rest.CompanyList out of START_ARRAY token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of ir.pisys.rest.CompanyList out of START_ARRAY token
So I have some questions here:
1- Is the first approach an optimized one? (Compared to others)
2- How can I fix the two other approaches?
The second and third approaches should work fine.
You need to check your json response structure.
You could use following jsons for tests (they work with your code):
Second approach:
[{"tseCIsinCode":null,"tseSIsinCode":null,"coName":"n1","industryID":0,"coID":0,"coNameEnglish":null,"companySymbl":null,"coTSESymbl":null,"groupID":0,"groupName":null,"industryName":null,"industryCode":null,"marketID":0,"marketName":null},{"tseCIsinCode":null,"tseSIsinCode":null,"coName":"n2","industryID":0,"coID":0,"coNameEnglish":null,"companySymbl":null,"coTSESymbl":null,"groupID":0,"groupName":null,"industryName":null,"industryCode":null,"marketID":0,"marketName":null}]
Third:
{"companyList":[{"coName":"n1","coID":0,"coNameEnglish":null,"companySymbl":null,"coTSESymbl":null,"groupID":0,"groupName":null,"industryID":0,"industryName":null,"industryCode":null,"tseCIsinCode":null,"tseSIsinCode":null,"marketID":0,"marketName":null},{"coName":"n2","coID":0,"coNameEnglish":null,"companySymbl":null,"coTSESymbl":null,"groupID":0,"groupName":null,"industryID":0,"industryName":null,"industryCode":null,"tseCIsinCode":null,"tseSIsinCode":null,"marketID":0,"marketName":null}]}
Update:
Second approach fix:
Change your json fields name - "CoName" -> "coName", "CoID" -> "coID" and so on. After that changes it will work pirfectly.
Third approach fix:
Wrap your json with "{\"companyList\":[...]
And change fields name as for second approach
Second Update
If you can't change json from response. You could use mapping in your Company class
#JsonProperty("CoName")
private String CoName;

Convert multiple Java Beans to JSON

I have multiple Java bean classes that are associated to each other (JSON Array + JSON Object) since they have a nested structure.
There are about 10 classes. Is there a way to collectively convert these classes or at-least one by one?
I had created these classes out of a JSON data which I don't have access to right now.
So, now, what I'm looking forward is to create a dummy JSON out of those classes.
Using GSON, I tried converting one of these Bean classes however, I got an empty result. Here is one of the beans called Attachment.java.
Attachment.java
package mypackagename;
import java.io.Serializable;
public class Attachment implements Serializable{
private Payload payload;
private String type;
public Payload getPayload() {
return payload;
}
public void setPayload(Payload payload) {
this.payload = payload;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Implementation
Gson gson = new Gson();
Attachment attachment = new Attachment();
String json = gson.toJson(attachment);
Sure you got an empty result. Because your JSON object is empty. You should add data to your object and test it again as below:
Attachment attachment = new Attachment(new Payload("Test Payload"), "Test attachment");
String json = new Gson().toJson(attachment);
Log.e("Test", "Json: " + json); // result: Json: {"payload":{"test":"Test Payload"},"type":"Test attachment"}
To avoid empty object, you have to set a default value to your payload and type becaus Gson will ignore any null value.
This section of the Gson User Guide: https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects
The fourth bullet point explains how null fields are handled.

Custom response on bad request using spring RestController

I have the following controller. I am using Spring to create Restful APIs.
#RestController
public class UserController extends RestControlValidator {
#RequestMapping(value = "/user/", method = RequestMethod.POST, headers = "Accept=application/json", consumes = "application/json", produces = MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody List newUser(#RequestBody #Valid UserInput input,BindingResult result)
{Some code}
}
The UserInput class looks like this:
public class UserInput{
#NotEmpty
private String emailId;
#NotEmpty
private String fName;
private String lName;
private int sex;
//getters and setters
Now when I try and access /user/ with data {"sex":"Male"}, I get the following response:
I want the response in case of such a request to be:
{"errors":{"sex":"The value must be an integer"}}
Is there any way of customising BAD REQUEST responses in Spring?
Considering the current scenario the most ideal solution would be to alter the behavior of HandlerMethodArgumentResolve as the json to pojo constructed by #RequestBody fails because we dont get a chance to check the wrong data and this check can very well be done in the custom message converter
A. first we would need to create LanguageMessageConverter as follows
public class LanguageMessageConverter extends
AbstractHttpMessageConverter<Language> {
private Gson gson = new Gson();
public LanguageMessageConverter() {
super(new MediaType("application", "json", Charset.forName("UTF-8")));
}
#Override
protected boolean supports(Class<?> clazz) {
return Language.class.equals(clazz);
}
Map<String, String> mp = new HashMap<>();
#Override
protected Language readInternal(Class<? extends Language> clazz,
HttpInputMessage httpInputMessage) throws IOException,
HttpMessageNotReadableException {
Map langmp = gson.fromJson(
convertStreamToString(httpInputMessage.getBody()), Map.class);
for (Field field : clazz.getDeclaredFields()) {
if (!langmp.get(field.getName()).getClass().getCanonicalName().equals(field.getType().getCanonicalName())) {
if (field.getType().getCanonicalName().equals("java.lang.Integer")||field.getType().getCanonicalName().toString().equals("int")) {
langmp.put(field.getName(), "0");
} else if (field.getType().equals("java.lang.String")) {
//TODO COde needs to be improved here because this check is not efficient
langmp.put(field.getName(), "wrong");
}
}
}
Language lang = gson.fromJson(gson.toJson(langmp), clazz);
return lang;
}
we need to set the media type new MediaType("application", "json", Charset.forName("UTF-8")) which will make sure this class intervenes the mentioned MIME type
Considering we need to manipulate the result I found it best to convert it to map langmp (There are better JSON Parsers which can be used)
Since we need to to understand the existing type I used reflection api to get the fields via getDeclaredFields()
Using the above made the logical check using the datatype to understand if the type is incorrect for eg if the field datatype is int and if it is found as String then corresponding map value will be substituted
once that is done the map will hold the updated values where in if the data was wrong a default value would be set eg if the int var is set to 0 since the originating json had a String in it.
Once that is done the updated map is converted to the concerned class.
B. Secondly we need to register the custom MessageConverter in the dispatcher xml i.e. LanguageMessageConverter
<mvc:annotation-driven >
<mvc:message-converters register-defaults="true">
<bean class="com.comp.org.controller.LanguageMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
register-defaults="true" is very important since we are adding Custom MessageConverter but we also need the other existing converters working along with the one we have added
LanguageMessageConverter needs to be registered here.
C. Considering the concerned pojo is populated with the necessary details it would reach our controller post processing in the custom converter now we would add the manual validation eg. if the int variable has 0 the necessary error json should be returned
As per your request even if the json consists of the wrong data the custom message converter should process it and accordingly in the controller we can validate the condition mentioned.
The code definitely can be improved further. Kindly let me know if this solution fulfilled your requirement or any part of the code requires further elaboration and hopefully addressed your concern.
I had the same issue, than I solved that way:
Create an Object called Error, like that (don't forget to implement Serializable...):
private String fieldName;
private String errorCode;
private String defaultMessage;
public Error() {
}
public Error(String fieldName, String errorCode, String defaultMessage) {
this.fieldName = fieldName;
this.errorCode = errorCode;
this.defaultMessage = defaultMessage;
}
/* getters, setters */
Inside the #RestController method you ave to call inputValidator.validate() method (if you didn't create an Object Validator for your UserInput then we're really don't speaking the same language...)
// validating the userInput
userInputValidator.validate(userInput, bindingResult);
if (bindingResult.hasErrors()) {
List<Error> errors = new ArrayList<>(bindingResult.getErrorCount());
for (FieldError fieldWithError : bindingResult.getFieldErrors()) {
errors.add(new Error(fieldWithError.getField(), fieldWithError.getCode(), fieldWithError.getDefaultMessage()));
}
return errors;
}
// in case of success:
return null;
Finally you'll have to translate the JSON object to your client side. You'll have two kind of objects:
3.1. null (undefined depending on the language you're using)
3.2. A JSON object like that:
[
{
"fieldName": "name",
"errorCode": "user.input.name.in.blank",
"defaultMessage": "Insert a valid name!"
},
{
"fieldName": "firstPhone",
"errorCode": "user.input.first.phone.blank",
"defaultMessage": "Insert a valid first phone!"
}
]

how to easily convert a JSON to a string for logging purposes

I will try to explain what I'm trying to accomplish as clear as I can.
I'm writing the back end of a web application, that uses REST APIs for extracting data that is used for reports in the client side.
The framework I'm writing on uses Codehaus jackson for parsing the requests from JSON to data objects (data beans).
I have a bunch of APIs of this sort, 10-15.
each of them gets a different request object from the client side (though some inheritance do exist).
what I want to do is add logging (using log4j) for each of these APIs so that when I enter each of the methods the request data object will be logged.
the simple solution would be to implement a toString() method for each of these data objects, but I want to avoid going over all of these data objects and see if there's a solution similiar to the way that jackson parses the JSON into an object.
I.e have the object converted back to its textual format in order to put it into the log.
I assume there's so easy way to do so.
This is an example of a REST API and its data bean:
#POST
#Path("/path123/")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public Response getSomeData(#Context HttpServletRequest httpServletRequest, DataBeanExample bean){
DataBeanExample resultBean;
//DO SOME STUFF , for example take the bean, extract parameters from it and call some other api.
return Response.ok(resultBean, MediaType.APPLICATION_JSON).build();
}
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "DataBeanExample")
public class DataBeanExample{
#XmlElement(name = "timeFrame", required = true)
private String timeFrame = "";
#XmlElement(name = "timeFrom",required = true)
private long timeFrom;
#XmlElement(name = "timeTo",required = true)
private long timeTo;
public String getTimeFrame() {
return timeFrame;
}
public void setTimeFrame(String timeFrame) {
this.timeFrame = timeFrame;
}
public long getTimeTo() {
return timeTo;
}
public void setTimeTo(long timeTo) {
this.timeTo = timeTo;
}
public long getTimeFrom() {
return timeFrom;
}
public void setTimeFrom(long timeFrom) {
this.timeFrom = timeFrom;
}
}
In the example, what I want to do is at the beginning of "getSomeData" take the object bean and have it logged.
Probably this might work
new ObjectMapper().writeValue(System.out, dataBeanExampleInstance)
or writeValueAsString
Do you know about:
JSON.stringify

Categories

Resources