I'm trying to get a JSON object displayed via an API, but I get a error message along with it.
Here is the method that I've written:
public MarketDataListLevel1 getMarketDataTicker() {
try {
MarketDataListLevel1 md = CS.getMarketDataTicker();
log.info(md.toString());
return md;
}
catch ( Exception e) {
e.printStackTrace();
return null;
}
}
Where CS is an object that processes the JSON data from a URL..
For some reason, when I call that method, I'm getting the precise output that I want, but I also get these additional error messages that follow it, and I'm unsure why. Any help would be appreciated.
14:52:55.265 [default] [main] ERROR si.mazi.rescu.JSONUtils - Error unmarshalling from
json: {"Bid":{"Price":183.25,"Size":0.0,"ExchangeID":"SMART","timeStamp":0},"Ask":{"Price":185.0,"Size":0.0,"ExchangeID":"SMART","timeStamp":0},"Last":{"Price":182.44,"Size":4.0,"ExchangeID":"SMART","timeStamp":0}}
java.lang.RuntimeException: Problem getting JSON object
at (Skipped all these logs)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.xeiam.xchange.cs.dto.MarketData[] out of START_OBJECT token
at [Source: java.io.StringReader#5069bb01; line: 1, column: 2]
This is the format of the data what I want returned:
{"Bid":{"Price":204.08,"Size":1.0,"ExchangeID":"SMART","timeStamp":0},
"Ask":{"Price":203.2,"Size":0.0,"ExchangeID":"SMART","timeStamp":0},
"Last":{"Price":204.64,"Size":2.0,"ExchangeID":"SMART","timeStamp":0}}
And it seems to be fulfilling that.
MarketDataLevel1 is defined as:
public class MarketDataListLevel1 {
public MarketData[] MarketDataListLast;
public MarketData[] MarketDataListBid;
public MarketData[] MarketDataListAsk;
public MarketDataListLevel1(#JsonProperty("Bid") MarketData[] MarketDataListBid,#JsonProperty("Ask") MarketData[] MarketDataListAsk,#JsonProperty("Last") MarketData[] MarketDataListLast) {
this.MarketDataListBid = MarketDataListBid;
this.MarketDataListAsk = MarketDataListAsk;
this.MarketDataListLast = MarketDataListLast;
}
And MarketData as
public class MarketData {
public BigDecimal Price;
public BigDecimal Size;
public String ExchangeID;
public long timeStamp;
public MarketData(
#JsonProperty("Price") BigDecimal Price,
#JsonProperty("Size") BigDecimal Size,
#JsonProperty("ExchangeID") String ExchangeID,
#JsonProperty("timeStamp") long timeStamp) {
this.Price = Price;
this.Size = Size;
this.ExchangeID = ExchangeID;
this.timeStamp = timeStamp;
}
}
Related
I'm attempting to convert the JSON output from my session and map it to a class that I've created using JAVA's ObjectMapper. When I run my tests on Lambda I get a Deserialisation error:
Deserialization error: com.amazon.ask.exception.AskSdkException
com.amazon.ask.exception.AskSdkException: Deserialization error
at com.amazon.ask.util.impl.JacksonJsonUnmarshaller.unmarshall(JacksonJsonUnmarshaller.java:50)
at com.amazon.ask.impl.AbstractSkill.execute(AbstractSkill.java:44)
at com.amazon.ask.AlexaSkill.execute(AlexaSkill.java:22)
at com.amazon.ask.SkillStreamHandler.handleRequest(SkillStreamHandler.java:71)
Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'AnswerIntent' as a subtype of [simple type, class com.amazon.ask.model.Request]: known type ids = [Alexa.Presentation.APL.UserEvent, AlexaHouseholdListEvent.ItemsCreated, AlexaHouseholdListEvent.ItemsDeleted, AlexaHouseholdListEvent.ItemsUpdated, AlexaHouseholdListEvent.ListCreated, AlexaHouseholdListEvent.ListDeleted, AlexaHouseholdListEvent.ListUpdated, AlexaSkillEvent.SkillAccountLinked, AlexaSkillEvent.SkillDisabled, AlexaSkillEvent.SkillEnabled, AlexaSkillEvent.SkillPermissionAccepted, AlexaSkillEvent.SkillPermissionChanged, AudioPlayer.PlaybackFailed, AudioPlayer.PlaybackFinished, AudioPlayer.PlaybackNearlyFinished, AudioPlayer.PlaybackStarted, AudioPlayer.PlaybackStopped, Connections.Request, Connections.Response, Display.ElementSelected, GameEngine.InputHandlerEvent, IntentRequest, LaunchRequest, Messaging.MessageReceived, PlaybackController.NextCommandIssued, PlaybackController.PauseCommandIssued, PlaybackController.PlayCommandIssued, PlaybackController.PreviousCommandIssued, SessionEndedRequest, System.ExceptionEncountered] (for POJO property 'request')
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.amazon.ask.model.RequestEnvelope$Builder["request"])
at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
at com.fasterxml.jackson.databind.DeserializationContext.invalidTypeIdException(DeserializationContext.java:1628)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownTypeId(DeserializationContext.java:1186)
at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._handleUnknownTypeId(TypeDeserializerBase.java:291)
at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:162)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:113)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:97)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:254)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeSetAndReturn(MethodProperty.java:151)
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.vanillaDeserialize(BuilderBasedDeserializer.java:269)
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserialize(BuilderBasedDeserializer.java:193)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3972)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2264)
at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:2746)
at com.amazon.ask.util.impl.JacksonJsonUnmarshaller.unmarshall(JacksonJsonUnmarshaller.java:48)
... 3 more
I did checks to ensure that my "riddleItem" variable is not null. The JSON values are being mapped to the Person class which just returns properties of a person. The code is shown below and I've highlighted the line which the error occurs on:
public Optional<Response> handle(HandlerInput input) {
Map<String, Object> sessionAttributes = input.getAttributesManager().getSessionAttributes();
System.out.println("This a FIRST debug");
LOG.debug("This a FIRST debug");
boolean correctAnswer;
String speechText = null, response;
System.out.println("This a SECOND debug");
Map<String, String> riddleItem = (LinkedHashMap<String, String>)sessionAttributes.get(Attributes.RIDDLE_ITEM_KEY);
Person person;
// System.out.println("riddleItem " + riddleItem);
if(riddleItem != null)
{
person = MAPPER.convertValue(riddleItem, Person.class); // ERROR OCCURS ON THIS LINE
}
System.out.println("This a THIRD debug");
PersonProperty personProperty = PersonProperty.valueOf((String) sessionAttributes.get(Attributes.RIDDLE_PROPERTY_KEY));
int counter = (int) sessionAttributes.get(Attributes.COUNTER_KEY);
int riddleGameScore = (int) sessionAttributes.get(Attributes.RIDDLE_SCORE_KEY);
System.out.println("This a FOURTH debug");
IntentRequest intentRequest = (IntentRequest) input.getRequestEnvelope().getRequest();
correctAnswer = compareSlots(intentRequest.getIntent().getSlots(), getPropertyOfPerson(personProperty, person));
System.out.println("This a FIFTH debug " + correctAnswer);
if(correctAnswer)
{
riddleGameScore++;
response = getSpeechExpressionCon(true);
System.out.println("This a SIXTH debug " + response);
sessionAttributes.put(Attributes.RIDDLE_SCORE_KEY, riddleGameScore);
}
else
{
response = getSpeechExpressionCon(false);
System.out.println("This a SEVENTH debug " + response);
}
AnswerIntentHandler setup = new AnswerIntentHandler();
//
if(riddle.getAnswer() != null)
{
speechText = "Hello " + riddle.getAnswer();
}
return input.getResponseBuilder()
.withSimpleCard("RiddleSession", speechText)
.withSpeech(speechText)
.withShouldEndSession(true)
.build();
}
[Json Output of properties under "riddleItem" during Session]1
I know my the values being mapped aren't empty thus I'm at a complete loss of ideas as to what's going on as I've come up short with possible ideas as to what the problem might be.
I solved the problem as I came to realise that when mapping from JSON to a class, methods ('set' methods) for assigning the JSON values to the variables in the class must be created. A sample structure for example:
public class State {
public State() {}
public State(String name, String abbreviation, String capital, String statehoodYear, String statehoodOrder) {
this.name = name;
this.abbreviation = abbreviation;
this.capital = capital;
this.statehoodYear = statehoodYear;
this.statehoodOrder = statehoodOrder;
}
public String getName() {
return name;
}
public String getAbbreviation() {
return abbreviation;
}
public String getCapital() {
return capital;
}
public String getStatehoodYear() { return statehoodYear; }
public String getStatehoodOrder() {
return statehoodOrder;
}
public void setName(String name) {
this.name = name;
}
public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
public void setCapital(String capital) {
this.capital = capital;
}
public void setStatehoodYear(String statehoodYear) {
this.statehoodYear = statehoodYear;
}
public void setStatehoodOrder(String statehoodOrder) {
this.statehoodOrder = statehoodOrder;
}
}
The declaration of an empty constructor is necessary when making use of multiple constructors where, one is parametric. In some cases without the inclusion of such constructor an error may be thrown so, to avoid the possibility of said error, adding the constructor as a "Dummy" so to say, is essential.
So I'm using retrofit to execute my API calls, and I have this endpoint which is returning error (which is the desired response), when I try to use my converter to turn the response.errorBody() into my java object Error, it returns the new Error object but its fields are null.
Here's my Error object class:
public class Error {
private final String message;
private final Object objectError;
public Error(String message, Object objectError) {
this.message = message;
this.objectError = objectError;
}
public String getMessage() {
return message;
}
public Object getObjectError() {
return objectError;
}
}
Here's my converter method:
public static Error parseError(Response<?> response) {
Converter<ResponseBody, Error> converter = ApiClient.retrofit().responseBodyConverter(Error.class, new Annotation[0]);
Error error;
try {
error = converter.convert(response.errorBody());
} catch (IOException e) {
return new Error("Connection Error", null);
}
return error;
}
And my JSON response looks alright:
{"status":"error",
"error":{"message":"# is an invalid character",
"objectError":"# is an invalid character"}}
Can you please help me find out what's happening?
Massive thanks!
I finally solved it by creating another class that has the status field and the error field, and used that one in the converter, the error field in that class is the one above.
Is there any simple methods to return exception in JSON using Rest api?
I've already googled this question, but all solutions i see, was about throwing exceptions during some calculations. But what if income parameters are wrong? I mean what if there is sone string instead of int input parameter?
I created some DTO class for input data:
#XmlRootElement
public class RequestDTO implements Serializable{
private static final long serialVersionUID = 1L;
#XmlElement(name = "request_id")
private String requestId;
#XmlElement(name = "site")
private List<String> sitesIds;
#XmlElement(name = "date_begin")
#JsonSerialize(using = DateSerializer.class)
#JsonDeserialize(using = DateDeserializer.class)
private Date dateBegin;
#XmlElement(name = "date_end")
#JsonSerialize(using = JsonDateSerializer.class)
#JsonDeserialize(using = JsonDateDeserializer.class)
private Date dateEnd;
#XmlElement(name = "volume")
private double volume;
// there is getters and setters
}
If i sent something like 'qwerty' instead of 'volume' field in my json request i'l see error message like Runtime. Is it possible to handle it in someway? I mean to return error in json with such structure?
public class ExceptionDTO {
private String shortExceptionMessage;
private String stackTrace;
public ExceptionDTO(String shotExceptionMessage, String stackTrace){
this.shortExceptionMessage = shotExceptionMessage;
this.stackTrace = stackTrace;
}
public String getShortExceptionMessage() {
return shortExceptionMessage;
}
public String getStackTrace() {
return stackTrace;
}
}
UPD1:
#Provider
#Singleton
public class ExceptionMapperProvider implements ExceptionMapper<Exception>{
#Override
public Response toResponse(final Exception e) {
StringBuilder trace = new StringBuilder();
IntStream.range(0, e.getStackTrace().length)
.forEach(i -> trace.append(e.getStackTrace()[i]).append('\n'));
ExceptionDTO exceptionMessage = new ExceptionDTO(
e.toString(),
trace.toString()
);
return Response.status(500).entity(exceptionMessage).build();
}
}
As it's not really clear if you are interested on checking if field or value of the payload is correct, here are a few ways to work with both.
If you want to check if the value for a field is correct (ie volume field value should be greater than zero etc), check out bean validation. This makes use of annotations on the fields you want to verify.
// for example
#Min(value = 0, message = "invalid message")
private double range;
To use your ExceptionDTO as error response whenever one of those validation fails, you can do so by creating an ExceptionMapper<ConstraintViolationException>. check it here for more details.
If you are checking for the invalid field (ie client sends ragne fields instead of range), have a look at the stack trace on what exception is being thrown. Then register an exception mapper with your ExceptionDTO as body.
For example, if UnrecognizedPropertyException is thrown then you can add:
#Provider
public class UnrecognizedPropertyExceptionMapper implements ExceptionMapper<UnrecognizedPropertyException> {
#Override
public Response toResponse(UnrecognizedPropertyException e) {
ExceptionDTO myDTO = // build response
return Response.status(BAD_REQUEST).entity(myDTO).build();
}
}
If you want to validate input parameters in the request, you should return status code 400 (Bad Request) along with the error details. You can simply send json
{ "error": { "message": "string received for parameter x, where as int expected" } with the response status code 400.
`
I did a bit of research and determined that the best way to encode a Java exception in JSON is to use a convention developed by Oasis that looks like this:
{
"error": {
"code": "400",
"message": "main error message here",
"target": "approx what the error came from",
"details": [
{
"code": "23-098a",
"message": "Disk drive has frozen up again. It needs to be replaced",
"target": "not sure what the target is"
}
],
"innererror": {
"trace": [ ... ],
"context": [ ... ]
}
}
}
details is a list that should have an entry for each nested cause exception in the chain.
innererror.trace should include the stack trace if you wish, as a list of string values.
The response status code should be 400 unless you have a good reason for making it something else, and the code in the structure should match whatever you sent.
Write one method to convert a Java exception to this format, and you are done. Use it consistently and your JS code will be able to handle and display the exception values.
More of the details of the other approaches evaluated and dismissed are covered in this blog post on JSON REST API – Exception Handling
https://agiletribe.purplehillsbooks.com/2015/09/16/json-rest-api-exception-handling/
Here is the java method to convert an exception to this format:
public static JSONObject convertToJSON(Exception e, String context) throws Exception {
JSONObject responseBody = new JSONObject();
JSONObject errorTag = new JSONObject();
responseBody.put("error", errorTag);
errorTag.put("code", 400);
errorTag.put("target", context);
JSONArray detailList = new JSONArray();
errorTag.put("details", detailList);
String lastMessage = "";
Throwable runner = e;
while (runner!=null) {
String className = runner.getClass().getName();
String msg = runner.toString();
runner = runner.getCause();
JSONObject detailObj = new JSONObject();
detailObj.put("message",msg);
int dotPos = className.lastIndexOf(".");
if (dotPos>0) {
className = className.substring(dotPos+1);
}
detailObj.put("code",className);
System.out.println(" ERR: "+msg);
detailList.put(detailObj);
}
JSONObject innerError = new JSONObject();
errorTag.put("innerError", innerError);
JSONArray stackList = new JSONArray();
runner = e;
while (runner != null) {
for (StackTraceElement ste : runner.getStackTrace()) {
String line = ste.getFileName() + ":" + ste.getMethodName() + ":" + ste.getLineNumber();
stackList.put(line);
}
stackList.put("----------------");
runner = runner.getCause();
}
errorTag.put("stack", stackList);
return responseBody;
}
I am strugling with my first Ckan application and from the reading i did i choose to use Gson. From Ckan, for testing, i try to get the user list by http://192.168.1.2:5000/api/action/user_list
This gives me
{"help":"http://192.168.1.2:5000/api/3/action/help_show?name=user_list",
"success":true,
"result":[
{ "openid":null,
"about":null,
"display_name":"default",
"name":"default",
"created":"2015-06-09T22:17:22.228196",
"email_hash":"d41d8cd98f00b204e9800998ecf8427e",
"sysadmin":true,
"activity_streams_email_notifications":false,
"state":"active",
"number_of_edits":0,
"fullname":null,
"id":"d5e49a3d-599d-49f3-9e20-826a03540357",
"number_created_packages":0
}, (..more data here...deleted for convenience)
{ "openid":null,
"about":null,
"display_name":"visitor",
"name":"visitor",
"created":"2015-06-09T22:16:52.785325",
"email_hash":"d41d8cd98f00b204e9800998ecf8427e",
"sysadmin":false,
"activity_streams_email_notifications":false,
"state":"active",
"number_of_edits":0,
"fullname":null,
"id":"9d279d8a-c068-46a5-9516-ed9c8de2f13b",
"number_created_packages":0
}
]
}
My problem is how to read this using Java.
What I did was
class JsonUserReply{
public String help;
public boolean success;
public JsonUsers[] userArray;
JsonUserReply(){}
}
class JsonUsers{
public String openid;
public String about;
public String display_name;
public String name;
public String created;
public String email_hash;
public boolean sysadmin;
public boolean act_email_notif;//"activity_streams_email_notifications"
public String state;
public int edits;
public String fullname;
public String id;
public int numb_cre_packs;//"number_created_packages"
JsonUsers(){}
}
I use the first class because the data provided is "some data, {array of Objects}".
Folowing some tutorials i used this:
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<JsonUserReply>>(){}.getType();
Collection<JsonUserReply> enums = gson.fromJson(host, collectionType);
This gives me an error on the fromJson command of:
Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
and the same error i get using this
JsonUserReply myTypes = gson.fromJson(host,JsonUserReply.class);
Edit
JsonUserReply myTypes = gson.fromJson(host,JsonUserReply.class);
throws Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
while digging around i found and used this*
url = new URL(host); //("https://graph.facebook.com/search?q=java&type=post");
InputStream is = url.openStream();
JsonParser parser = Json.createParser(is);
while (parser.hasNext()) {
Event e = parser.next();
if (e == Event.KEY_NAME) {
switch (parser.getString()) {
case "name":
parser.next();
System.out.println("Name "+parser.getString());
break;
case "result":
System.out.println("RESULT1 "+parser.getString());
e=parser.next();
System.out.println("RESULT2 "+e.toString());
break;
default:
System.out.println(parser.getString());
break;
}
}
which even if it is way too complicated to use for my data as they are, it revieled something intresting. inside the case "result": i re-read the parser just to check whats its value. And it turns out that
RESULT1 result
RESULT2 START_ARRAY
RESULT3 START_OBJECT
To me that means that the json object is sending the data with the keyword"START_ARRAY" but Gson is expecting BEGIN_ARRAY. Am i right? apparently these two are not the same. how can i use the Gson way to get the array from my data? Thanks
I am trying to deserialize/map the below JSON to List<Bill> java object using Jackson json library. (this json was generated by jackson, Iam omitting that piece for brevity)
{"bills":[{"amount":"13","billId":"billid3"}]}
Here is my conversion method:
private static void convert(){
String jsonBill = "{\"bills\":[{\"amount\":\"13\",\"billId\":\"billid3\"}]}";
ObjectMapper mapper = new ObjectMapper();
List<Bill> bills = null;
try {
bills = mapper.readValue(jsonBill, new TypeReference<List<Bill>>() { });
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("bills = " + bills.size());
}
The Bill entity is below:
#JsonTypeInfo(use=JsonTypeInfo.Id.CLASS)
public class Bill {
private String amount;
private String billId;
public String getBillId() {
return billId;
}
public void setBillId(String billId) {
this.billId = billId;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
and I get this error:
**org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token
at [Source: java.io.StringReader#7a84e4; line: 1, column: 1]**
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:194)
at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:103)
at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:93)
at org.codehaus.jackson.map.deser.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1980)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1278)
Here is my simplified spring3 controller which returns the i/p json (with Jackson mapping configured as default view):
#ModelAttribute("bills")
#RequestMapping(value = "/", method = RequestMethod.GET)
public List<Bill> fetchBills() throws IOException {
Bill bill = new Bill();
bill.setAmount("13");
bill.setBillId("billid3");
List<Bill> bills = new ArrayList<Bill>();
bills.add(bill);
return bills;
}
I guess I am missing something obvious.. but not sure what it is.. Any ideas?
The problem lies not in your code, but your example input. What you're actually trying to deserialize is an object with a field named "bills", not a list! What you should be using as input is:
[{"billId":"billid3","amount":"13"}]
This is an array of objects, which is converted to a list.
Try using ObjectWriter instead of ObjectMapper
Writer writer=new StringWriter();
ObjectWriter oWriter=om.writerWithType(new TypeReference<List<Bill>>() {
});
oWriter.writeValue(writer, result);
I'm using jackson 1.9.2