Convert Spring webclient error response body from string to object - java

In this method the msg variable is returning in string format..how can be conversion of it can be taken place into certain java pojo object.
Method image
In place of Mono I tried Mono but it didnt worked for me.
I just want to get the error response body in pojo object format rather then in string format.
Also tried in this manner, but no success.
Tried in this manner

do you know what class the response is in case of an error? ResponseEntity perhaps?
you need to use a JSON converter package to convert it into a POJO, but you first need to make a POJO class of your own with the relevant fields (and make sure to name them in case sensitive manner).
then, inside the .flatMap(...) turn the String into the POJO using the converter.

Related

Java Spring #ResponseBody return XML with produce attribute

Recently, I'm trying to get the xml file under the resources file. Then all the questions come out.
I read so many articles say something like:
#ResponseBody tells a controller that the object returned is
automatically serialized into JSON and passed back into the
HttpResponse object.
So my question is: the produces attribute is used to narrow the mapping by the media types that can be produced by the mapped handler, not to declare the media types the mapped handler will produce, right? Then how should I return xml content in the condition of automatically serialized into JSON format? I guess I mess up the meaning of the content showing in the view with HTTP response body... Please someone explain to me nicely and patiently :"(

Test putting badly formed json using JerseyInvocation builder

I'm trying to write an integration test to ensure that my dropwizard application is returning appropriate error codes. One of the scenarios includes ensuring that if the object being PUT is not deserialisable, I get a reasonable error response.
I can't find a way using JerseyInvocation.Builder to PUT a badly-formed JSON entity. The only way to create an Entity appears to be by having an object of the correct type, which obviously will deserialise. I want to be able to create an entity whose serialised value is an arbitrary string that I provide.
I've tried putting objects of the wrong type, but that doesn't test all of the edge cases I want to test. I also want to avoid creating a lot of types that are subtly different from the object the API is expecting.
Can anyone suggest a way of achieving what I want?
Update: this is the code I'm using at the moment:
JerseyInvocation.Builder request; // initialised elsewhere and not interesting
is = new ClassPathResource(nameOfFileContainingWellFormedJson, MyEntityClsss.class).getInputStream();
entity = Entity.entity(is, MediaType.APPLICATION_JSON);
request.put(entity);
This response should be returning 200 and is returning 400.
is = new ClassPathResource(nameOfFileContainingBadlyFormedJson, MyEntityClsss.class).getInputStream();
entity = Entity.entity(is, MediaType.APPLICATION_JSON);
request.put(entity);
This response is returning 400, but until the above is returning 200 it's not doing so for the right reasons.
You can use the Jersey Entity class, used for raw streams of any mime-type. Something like:
ByteArrayInputStream bais = new ByteArrayInputStream("{\"malformattedJson".getBytes());
builder.post(Entity.entity(bais, "application/json"));
Or load the malformed JSON from a file/classpath resource in your tests to ditch the double quote escaping 😉

Is there some way for a Jackson Delegate-based Creator to access the raw Json String?

Is there some way for a Jackson Delegate-based creator to access the raw Json String?
#JsonCreator
private static MyClass createFromJson(Map<String, Object> jsonProperties) {
return new MyClass(rawJson);
}
I am able to get the raw input as a Map of Strings to Objects in the code above, but I want to be able to access the json as a string. I tried the code below (based off of http://www.cowtowncoder.com/blog/archives/2011/07/entry_457.html) but that code as written is never invoked.
#JsonCreator
private static MyClass createFromJson(String rawJson) {
return new MyClass(rawJson);
}
Note: This is a spring boot application (1.3.1.RELEASE) that uses Jackson 2.6.4.
Looks like this type of functionality would not make sense in this context. In fact, it appears to me now that requesting the JSON string in this instance defeats the purpose of using jackson in the first place. However if anyone finds themselves here, then the comments from Sotirios Delimanolis may be useful:
"Hack: you can receive a JsonNode as the parameter type and use its toString method to get the corresponding JSON."
"It looks like you want a JsonDeserializer"

Camel routing based on object value

I have two simple Camel routes working for writing to a jms queue and reading from it. I am putting a serialized object to the queue. I am able to deserialize it and covert it to json successfully.
Route for writing:
from("direct:message").to("jms:myqueu")
My route for reading:
from("jms:myqueu")
.marshal()
.json(JsonLibrary.Gson).
.to("file://cc")
Now i want to check a field within the object and route based on that.Also that field should not be part of the final json.
Can i check the value within the object and route based on that( like write to different files?). I can add the annotation in the pojo to avoid the field in final json
I thought of converting object to json, and then sending to queue. Then i can use jsonpath for conditional routing. But then how can i omit a field from final json?
Yes, you can use content based routing to check any field in incoming object and do the routing based on that.
ref: http://camel.apache.org/content-based-router.html
To ignore a field during json marshalling , you can use #JsonIgnore - Jackson annotation.
For reference, i was able to get it working. I added a custom filter class and checked the bean value within that. Also linked it within the choose option for routing.
My route is now:
from("jms:myqueu")
.choice()
.when()
.method(Exp.class,"checkfieldA")
.json(JsonLibrary.Gson).
.to("file://cc")
.when()
.method(Exp.class,"checkfieldB")
.json(JsonLibrary.Gson).
.to("file://dd")
.endChoice()
Here Exp is a normal class and checkfieldA and checkfieldB are two methods returning boolean values.
class Exp{
public boolean checkfieldA(Message message){
myobj obj = (myobj)message.getBody() // this is the object is put to queue. Need to cast to my object type .
if(myobj.isFieldA()){
return true;
}
}
}

Is case important while parsing JSON using GSON

I am parsing JSON data, and storing the results in a Java object using GSON. My question is, should the fields in the JSON String match the instance variables in the class? Including the class names? For eg,
If this is my JSON string -
"telephone":
{
"landline":"1-818-502 8310"
}
Should I have a class as below?
public class Telephone
{
private String landline;
}
The reason why I am asking this is, when I use gson's fromJson(obj), the object doesn't contain any values. It shows all records as null. I am wondering if it is throwing the error due to this.
Please note - This is not the entire code. My JSON data is quite huge, so I can't paste it here. The above telephone string is just one of the many embedded strings within my json string.
This is wrong JSON:
"telephone":{"landline":"1-818-502 8310"}
The JSON objects start with a { and end with a }. SO, it should be something like
{"name": "somename", "telephone":{"landline":"1-818-502 8310"}, ...}
Yes. Attributes in class should have exact same case and character as in the JSON String in case you are using default Gson instance as correctly mentioned by Eliran. Please note that you must have attributes just having getter/setter and not attribute wouldn't work.
You mentioned you are using inner class. It may not work with default Gson instance. You may need to use registerTypeAdapter like this:
gson.registerTypeAdapter(MyType.class, new MyInstanceCreator());
refer: https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization

Categories