Httpmessagenotreadableexception: Could Not Read Json: Unrecognized Field Using Spring for Android

HttpMessageNotReadableException: Could not read JSON: Unrecognized field using Spring boot and Android

As mentioned above - you can't have 2 or more @RequestBody. I recommend creating a wrapper class that will hold these parameters from these 2 classes of yours. Also get rid of this 'logic' from your controller layer... and get rid of these ifs... consider using pattern matching from vavr library since strategy pattern seems to be overkill there

HttpMessageNotReadableException: Could not read JSON: Unrecognized field using Spring for Android

The exception seems to suggest that while the Jackson mapper is trying to create a Loja from the returned json, it encountered a json field with a key of Loja, which it didn't know how to handle (because there are no fields in the Loja java object named "Loja"), so it failed. This seems logical, because it appears that the server is effectively returning this json structure:

{"Loja":{"nome":"teste","xValue":"20","yValue":"30","andar":"1"}}

Because of this, you have two primary options.

  1. Change the object that is passed to getForObject() or
  2. Change the json returned from the server.

For the first one, you can have your client do a getForObject() passing in an Object that contains a Loja.

Something like this class:

class MyObj {
private Loja Loja;

public void setLoja(Loja loja) {
this.Loja = loja;
}

public Loja getLoja() {
return this.Loja;
}
}

being used with this call:

restTemplate.getForObject(url, MyObj.class);

Alternatively, you could have your server return the Loja objects fields as a part of the model, or better yet, return the instance of the Loja object that you created. Spring is smart enough to utilize Jackson to turn your POJO's into the proper json model you are expecting.

@RequestMapping("/android/played")
public Loja getLoja() {
System.out.println("Android Resquest.");

return new Loja("teste", "20", "30", "1");
}

Which would produce this json:

{"nome":"teste","xValue":"20","yValue":"30","andar":"1"}

Which would be readable by your getForObject() method on the client side just the way it is currently.

trouble getting an object in a json output

As per your JSON, Your POJO classes should be:

public class RadioInfo {
private Response response;
// getters setters
}

public class Item {
private Integer episodeId;
private String type;
private String title;
//getter setters
}



public class Response {
private List<Item> items = new ArrayList<Item>();
private String nextUrl;
//getters setters
}

Unrecognized field in Android JSon deserialization

Solved

Server REST was returning Json with uppercase camel humped variables, e.g.

TotalGrand

renamed to

totalGrand

solved issue.

How to get to a JSON object in this output using spring for Android

According to your stack trace, you need to request permission to use internet. Try adding

<uses-permission android:name="android.permission.INTERNET" /> 

to your manifest, and after that, you also have to request the permission at runtime if your targeting API level 23 or after, look here for more info:
What permission do I need to access Internet from an android application?
https://developer.android.com/training/permissions/requesting.html

For the requesting permission at runtime, you can try adding the following to your onCreate in your MainActivity, for example

// Check permission for INTERNET
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.INTERNET) !=
PackageManager.PERMISSION_GRANTED) {
// Request Permissions Now
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.INTERNET},
MainActivity.REQUEST_INTERNET);
}

you can also look here:
http://inducesmile.com/android/android-6-marshmallow-runtime-permissions-request-example/

Jackson Json Deserialisation: Unrecognized field ... , not marked as ignorable

Another thing to check out is PropertyNamingStrategy, which would allow Jackson to use "Pascal naming" and match JSON properties with POJO properties. See f.ex here: http://www.javacodegeeks.com/2013/04/how-to-use-propertynamingstrategy-in-jackson.html

Eclipse compiler error after upgrading library version

Turns out that in the .classpath file which is located in the projects root, eclipse left the old version of the core-jackson jar, alongside with the new one, this was causing all the errors!

I am really going to use intelli-j next time I have to use Java...

<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/commons-codec-1.6.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.3.jar"/>
<classpathentry kind="lib" path="lib/fluent-hc-4.3.2.jar"/>
<classpathentry kind="lib" path="lib/httpclient-4.3.2.jar"/>
<classpathentry kind="lib" path="lib/httpclient-cache-4.3.2.jar"/>
<classpathentry kind="lib" path="lib/httpcore-4.3.1.jar"/>
<classpathentry kind="lib" path="lib/httpmime-4.3.2.jar"/>
//Just erased bellow line:
<classpathentry kind="lib" path="lib/jackson-core-asl-1.8.2.jar"/>
<classpathentry kind="lib" path="lib/jackson-core-asl-1.9.13.jar"/>
<classpathentry kind="lib" path="lib/jackson-mapper-asl-1.9.13.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

All credits to @Yuri Prezument :D



Related Topics



Leave a reply



Submit