i have a immutable enum class that i can't change but need to pass value into postman in the same format.
here below is the class.
public class Request {
private int num;
private Type type;
public Request(Type type, int num) {
this.type = type;
this.num = num;
}
public int getNum() {
return num;
}
public Type getType() {
return type;
}
public enum Type {
MEN, WOMEN , FAMILY, CHILD
}
}
i am looking for exact Json format which is pass through Postman. everytime i am getting 400 error while hitting the endpoint.
write a simple program where you can instantiate your Request class and serialize it to JSON String. That would be your JSON that you pass as param. But ultimately the API provider is responsible to tell you what input format is expected.
Here is how you can serialize your class to Json String: you can use JSON Jackson library and use method writeValueAsString Of class ObjectMapper. So, just instantiate your Request class and pass the instance to this method and you will get your Json String. Also I wrote my own JsonUtils utility that my be even simpler but will do the same. use static method JsonUtils.writeObjectToJsonString to serialize your class instance to JSON String. Class JsonUtils comes as part of Open source MgntUtils library. You can get this library as Maven artifact at Maven central or on Github.
Related
Newbie developer here. I am trying to make a call to a public API. The API receives the name of a drink as a string and returns information and recipe for that name. The response from the API looks like this:
{
"drinks":[
{
"id": ...
"name": ...
"recipe": ...
"category": ...
"alcoholic": ...
... many other fields ...
},
{
...
}
...
]
}
I am only interested in name, recipe and category. I have a domain class for this purpose that looks like this
#Data
#NoArgsConstructor
#AllArgsConstructor
#JsonIgnoreProperties(ignoreUnknown = true)
public class Drink {
#JsonProperty("name")
private String name;
#JsonProperty("category")
private String category;
#JsonProperty("recipe")
private String recipe;
}
I also implemented a client to call the endpoint using restTemplate. Here is the call that client makes:
ResponseEntity<List<Drink>> response = restTemplate.exchange(
url,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Drink>>() {
});
My goal is to call the API, get the response and only the fields that I want and store it in a list of Drink. However when I try to run the app locally and make a call I am getting this error:
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.ArrayList<Drink>` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<Drink>` from Object value (token `JsonToken.START_OBJECT`)
When I use ResponseEntity<String> instead, it works but returns the whole json as a string, which does not seem like a good approach. How can I get this approach to work?
The problem is mismatch between json structure and object structure. The object you deserialize into must represent correctly the json. It's an object with a field drinks, which is an array of objects(drinks in your case). Correct java class would be:
public class Wrapper {
private List<Drink> drinks;
//getters and setters
#Override
public String toString() {
return "Wrapper{" +
"drinks=" + drinks +
'}';
}
}
Other option would be to write custom deserializer, which can extract drinks from the tree before deserializing directly into a list.]
Edit: Added toString() override for debugging purposes.
Im trying to get the Json inside Json response of the rest api.
httpsConn.getInputStream() will be a Json like
"data":[
{"id":"1","name:"aaa","score":"90"},{"id":"2","name":"bbb","score":"85"}
]
Jave code:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
response = (MarkResponse)objectMapper.readValue(httpsConn.getInputStream(), MarkResponse.class);
Pojo class:
public class MarkResponse {
private int count;
private List<MarkData> markData;
//setter and getter.
}
public class MarkData {
private String id;
private String name;
}
The response is like below, as I'm using the List inside my main pojo.
{"headers":{},"body":"MarkResponse [count=2 markData=[MarkData [id=1,
name=aaa], MarkData
[id=2,name=bbb]]],"statusCode":"ACCEPTED","statusCodeValue":202}
What I'm expecting is,
{"headers":{},"body":"MarkResponse [count=2
markData={"id":"1","name:"aaa"},{"id":"2","name":"bbb"}],"statusCode":"ACCEPTED","statusCodeValue":202}
What is the code change i should make to get the expected output.
I think the problem is private fields. When I make fields public, jackson works and I don't need to objectMapper.disable(anything); when fields are private, or protected, or package-private, they are read, but not written to.
That is, assuming that you try to use org.codehaus.jackson.map.ObjectMapper (yes, not the latest version) rather than something else that defines a class named ObjectMapper.
This works after changing the return type.
My earlier response was,
public ResponseEntity<String> fnCall() {
//code here
return new ResponseEntity<String>(MarkResponse.toString(), HttpStatus.ACCEPTED);
}
I changed this to make it work.
public MarkResponse fnCall() {
//code here
return response;
}
I am using JAX-RS for RESTful web services. In doing so I am serializing an object to JSON. My problem is the JSON I get back contains a name attribute that does not exist on my original object. I would like to get rid of it. Example:
public class TestClass {
private string var1;
private int var2;
"getters and setters"
}
The JSON I get returned, once I instantiate, is:
{
"var1": "Hello World".
"var2": 7
"name": "TestClass"
}
How do I get rid of that name attribute?
I'm passing a JSON object from a PUT request to my server. The request itself works, however the fields in the JSON which have an underscore (snake_case) seem to bi ignored. The request outputs the received data to see what comes out, and the value with the underscore converts to camelCase, and doesn't get parsed. Here's the class:
Public User{
private int id;
private String name;
private int some_value;
}
The JSON object I pass to the PUT request:
{ "id":1, "name":John, "some_value":5 }
The PUT method only returns what MOXy caught in this case
#PUT
#Path("user")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public User addUser(User user){
return user;
}
And the output is:
{ "id":1, "name":John, "someValue":0 }
Notice how "some_value" changed to "someValue" and didn't get the actual value updated. Any idea on why this is happening?
MOXy follows Java Bean conventions by default, which suggest camel case. If you don't want to (or can't) use camel case, you can add an annotation to the field:
#XmlElement(name = "some_value")
private int some_value;
If you don't want to annotate all your fields, use an XMLNameTransformer.
I was looking for some solution around here and I didnt find any correct answer to my question so I would like to ask you.
I have POJO with some simple attribs. and one List of another POJOs.
public class Standard implements Serializable {
private String id;
private String title;
private String description;
private Set<Interpretation> interpretations = new LinkedHashSet<Interpretation>();
}
public class Interpretation implements Serializable {
private String id;
private String title;
private String description;
}
In my controller class, I am returning Standard POJO with GSON.
#RequestMapping(value="/fillStandard", method= RequestMethod.GET)
public #ResponseBody String getStandard(#RequestParam String id) {
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
return new Gson().toJson(s);
}
The question is, am I able to get the list of interpretations in my Standard POJO using jQuery ? Something like :
function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
alert(data.interpretations[0].title);
});
}
Thanks a lot !
EDIT:
Well, thanks to #Atticus, there is solution of my problem. Hope that it will help somebody.
#RequestMapping(value="/fillStandard", method= RequestMethod.GET, produces="application/json")
public #ResponseBody Standard getStandard(#RequestParam String id) {
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
return s;
}
Using #ResponseBody allows you to return the whole POJO, but you need to add produces="application/json" to your #RequestMapping annotation. Then you will be able to catch a returning object as JSON in jQuery like as I supposed.
function newStandard() {
$.get("standard/fillStandard.htm", {id:"idOfStandard"}, function(data) {
alert(data.id); //Standard id
alert(data.interpretations[0].title); //id of Interpretation on first place in array
});
Well you have to create and register your custom serializer.
It goes like this:
//You create your builder that registers your custom serializer with the class you want to serialize
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Standard.class, new StandardSerializer());
//Then you create your Gson object
Gson gson = builder.create();
//Then you pass your object to the gson like
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
gson.toJson(s);
Your serializer looks like this:
public class StandardSerializer implements JsonSerializer<Standard>{
#Override
public JsonElement serialize(Standard src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject obj = new JsonObject();
//You put your simple objects in like this
obj.add("id",new JsonPrimitive(src.getId()));
//You put your complex objects in like this
JsonObject interpretations = new JsonObject();
//Here you need to parse your LinkedHashSet object and set up the values.
//For the sake of simplicity I just access the properties (even though I know this would not compile)
interpretations.add("title", src.getInterpretation().getTitle());
obj.add("interpretations", interpretations);
return obj;
}
}
In this case your Json would look something like:
{"id":"idValue", "title":"titleValue", "description":"descriptionValue", "interpretations":["id":"interpretationIdValue"]}
Now, you can access your data with jQuery like this:
function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
alert(data.interpretations.title);
});
}
I hope this helps.
EDIT:
I see that your response gets converted to the declared method argument type which is String (as stated here: 16.3.3.2 Supported method return types). But what you really want is your Standrad POJO converted to JSON. I am not very familiar with Spring but as I have read here (16.3.2.6 Producible Media Types) there is another, maybe easier solution. If you want to return a JSON object, then change the return type of the
getStandard method to Standard instead of String and add produces="application/json" to your #RequestMapping annotation. As far as I have read this should tell Spring that the return type should be converted to JSON. In this case you do not need to use Gson.