Jackson ContextResolver is loaded but getContext method is not invoked in wildfly 18 - jax-rs

All java.util.Date objects are converted like "2020-09-23T09:53:06.93Z[UTC]" but i don't want to include timezone in the serialized date like "2020-09-23T09:53:06.93". So i decided to use Jackson ContextResolver. I have used #Provider annotation and set objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSS")). My ContextResolver is loaded but getContextMethod is not invoked.
Can you confirm whether i'm doing right approach? Also assist me to resole this.

Related

Spring Jackson java.util.Date issue

I am using Spring boot( > 2.3) + Jackson in my application. I have POJO class and have java.util.date property in it. I have an REST api exposed on this POJO. Now when I receive the response from Spring boot the Date property value is converted to UTC everytime.
To fix this I have an option to set #JsonFormat(timestamp="ist"), this can be done. But in my case I cannot provide this option as my project is not running in IST.Now i tried to create a Deserializer class and annotated with #JsonComponent. Even this is not picking while deserialization.
If I try with LocaldateTime, issue fix can be achieved. But in all project I am using java.util.date and have no time to change in all places. Also I tried to #Autowire ObjectMapper and set the timezone to the same. But just before sending the response i tried to print the response in the log and data is fine and correct but when spring send the response back to client the date changes to UTC.
Anyone please help me out....

fasterxml jackson JSON deserialization - custom adding

I use Jackson FasterXML product to deserialize JSONs. Now, I noticed in profiler that I got a ton of duplicate strings as a result since every time I receive a JSON it deserialize into an object which always contains some String variable which tells me the Type name. (it's answer to Facebook GraphQL query). Now, naturally, I would prefer for .intern() method to be used during deserialization of this specific method. Is that possible? If so, how?
Seems StringDeserializer can provide whttp://stackoverflow.com/questions/17041249/jackson-override-primitive-type-deserialization

SpringFox/Jackson doesn't parse JSON attribute

I'm using Spring boot with springfox and having problems parsing JSON payload from Swagger UI into Java class (JSON->POJO). No errors, but the resulting Java object is missing a field (null). The top class for the class with missing attribute has Mixin to switch datatypes. That part works fine.
I can't really debug since framework does most of the parsing. The req-d attribute is displayed in Swagger under Model Schema correctly. However, when I submit JSON payload containing same attribute, the corresp POJO's attribute is null.
Please steer me to the right direction.
I've figured it out my issue: I was missing setter for the field in the Mixin interface. The JSON was being parsed, but the setter was missing, hence null value.

Ninject Conditional injection problems

I have the following bindings declared
Bind<IDataSource>().To<DataSourceOne>();
Bind<ISettings>().To<DataSourceSettings>
.WhenInjectedInto<DataSourceOne>();
Bind<ISettings>().To<Settings>();
now I call
Kernel.Get<IDataSourc>();
Ninject correctly injects a DataSourceSettings, but I need to pass a constructor argument to Settings and DataSourceSettings based on data from a config file. so I've changed the IDataSouce binding as follows
Kernel.Bind<IDataSource>().To<DataSourceOne>()
.WithConstructorArgument("settings", Kernel.Get<ISettings>(
new ConstructorArgument("data", objectContainingConfigFileData)
)
);
in that case Ninject injects Settings class instead of DataSourceSettings class. I assume the problem is that the ISettings is getting resolved before it is injected into the DataSourceSettings class so Ninject does not use the binding I intended it to. Is there a way to get around this. I haven't found anything yet.
It should work if you define the constructor argument for the ISettings binding and not for the DataSource binding. Assuming you already know the object with the config file data in the module. Otherwise maybe a factory would be more appropriate.
kernel.Bind<IDataSource>().To<DataSourceOne>();
kernel.Bind<ISettings>().To<DataSourceSettings>()
.WhenInjectedInto<DataSourceOne>()
.WithConstructorArgument("data", objectContainingConfigFileData);
kernel.Bind<ISettings>().To<Settings>();

JSON.NET JsonIgnore DeserializeObject

I created a Web API using VS 2012. I have a method with a custom object parameter that I am passing JSON to via Fiddler for testing:
[HttpPost, HttpPut]
public HttpResponseMessage UpsertProject(Projects p)
{
...
}
My Projects object has about a dozen properties marked as JsonIgnore. My assumption was that when my object was serialized into Json those properties would be ignored...which is true. However, when I debug my method I'm noticing that all the object properties marked with JsonIgnore are set to null even if the Json that I pass in from Fiddler is setting them. I also try to get data as Json and deserialize it into a new instance of the object but that also does not set the properties that are marked JsonIngore. I knew JsonIgnore would work for serializing but didn't think it would prevent properties from being set when deserializing. What's frustrating is I know that ScriptIgnore doesn't behave this way, but I want to use JSON.net to handle my serializing/deserializing. I've also created a windows app and tested the same serializing/deserializing functionality and it works in it. So I'm wondering if this is a Web API limitation with the JsonIgnore attribute?
If it works the way you want in the Windows application but not in the Web API, that tells me that the JSON serializer settings are different between the two. What settings are you using in the Windows app that makes it work? You can take those settings and apply them to the Web API serializer in the Register method of the WebApiConfig class (in the App_Start folder of your Web API project). For example:
JsonSerializerSettings jsonSettings = config.Formatters.JsonFormatter.SerializerSettings;
jsonSettings.NullValueHandling = NullValueHandling.Ignore;
jsonSettings.DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate;
...
EDIT
OK, so if I understand you correctly, based on your most recent comments, you want everything to be deserialized if possible, but you only want two specific properties to be serialized and those apparently do not have null or default values. In that case, there are two approaches you can take:
Set the properties that you don't want serialized to null or zero or false (the default value) just before serializing. Because you have DefaultValueHandling set to Ignore, this will cause those properties not to be serialized.
Create several boolean ShouldSerializeXXX() methods in your class where XXX is the name of each property you don't want serialized. These methods should return false. See the first answer of this question for an example.
Don't use JsonIgnore because, as you have seen, this will cause the property to be completely ignored by Json.Net, both for serializing and deserializing.