How to use HttpServiceProxyFactory (Spring 6) for only a subset of JSON fields of a GET response? - spring

I have a Spring 6 client which invokes a RESTful Webservice with a GET request. The response contains a JSON record with more fields than needed. How can I use the new HttpServiceProxyFactory together with a data class which doesn't contain attributes for each field, but just attributes for the required fields. Any hint is appreciated.

Related

How to return model object from jpa query

I'm developing a Spring Boot application with Spring Data JPA. I want to return the promedio field when the calificacionSemestral1 field is a specific value and the calificacionSemestral2 field is a specific value.
Entity
DER
Doing this using JPQL is it possible?
I know that I can create a DTO with the same fields or do two queries, but I want to avoid doing it if I can get the result in a single query.

Mongo ObjectId backward conversion from JSON to POJO

in my database I have user with ObjectId("5f78cd195a52a201fb117175").
Then I send it by Spring REST Controller to Angular Frontend and there my object id looks like this:
{date: 1601752345000, timestamp: 1601752345}
Afterwards in frontend I create product object, which contains field userId whose value is set to {date: 1601752345000, timestamp: 1601752345} . That object is sent to backend and later on is saved in db. The problem is that when it is converted by Jackson in Rest controller the userId field has value ObjectId("5f78cd19065ece5ade441e7a").
So from user with ObjectId("5f78cd195a52a201fb117175") I receive ObjectId("5f78cd19065ece5ade441e7a")
I dont have user with this second ObjectId so the field with userId contains no realtion to real user.
DO you know why it happens and how to deal with it ?
Jackson treats all objects like POJO types. By default, serialises all getter-s. Let's assume, that ObjectId instance in Java is represented by org.bson.types.ObjectId which has two getter-s:
getDate in your case date field in JSON
getTimestamp in your case timestamp field in JSON
These two fields will be serialised to JSON payload. Deserialisation process needs constructor and setters. I guess, in this case constructor ObjectId​(int timestamp, int counter) will be chosen (but I am not sure). You can try to create new instance of ObjectId using this constructor with values from your JSON payload.
When you use Jackson to serialise/deserialise types from 3-rd party libraries you can expect that there is a module implemented for this. Take a look on MongoJack:
Since MongoDB uses BSON, a binary form of JSON, to store its
documents, a JSON mapper is a perfect mechanism for mapping Java
objects to MongoDB documents. And the best Java JSON mapper is
Jackson. ...
You need to register this module and serialisation/deserialisation processes should work as expected.
See also:
How do you extract a timestamp from a MongoDB ObjectId in Spring Data MongoDB?
Binary JSON with bson4jackson

Expose custom query in Spring Boot Rest API with multiple joins

I have an Spring REST Api and a MySQL Database, now I would like to expose the result of an custom query with multiple joins.
I have tried multiple suggestions that I found online but none of them were working for me so far.
What I want to do is something like a read only DTO that has all the fields of my custom query so that in the end I have one api page exposing the DTO data as JSON so my client (Angular) can read the data from there.
I already tried to:
create an #RestController with an injected EntityManager that executes a NativeQuery and then populates the DTO with the returned data but since my DTO is no Entity I get an Hibernate Mapping Exception
create a custom Repository and its Impl but with a similar outcome
place the Query inside an existing #Entity that is part of the Query statement
What am I missing here? Do I have to annotate my DTO maybe? Cuttently it's just a POJO, I think the #Entity annotation is not the right thing here since I don't want a Table created from my DTO.
Fixed it by letting the Query return an Array of type Object and afterwards mapping it to the DTO Constructor.

Cannot retrieve selected lookups using the OData endpoint

I'm unable to retrieve lookup fields when filtering an OData Request.
I used the following requests :
https://mycrm.api.crm4.dynamics.com/api/data/v9.1/contacts(guid)?$select=contactid,ownerid,createdby,new_expirefin,new_testcumul_stat```
This request retrieves contactid, new_expirefin, and new_testcumul_stat, but no trace of ownerid and createdby.
In another hand, this request:
https://mycrm.api.crm4.dynamics.com/api/data/v9.1/contacts(guid)
return all fields, including those missing on the other request. Lookups are sent as Guid.
Both request uses the
Prefer = odata.include-annotations="*"
header. Knowing I cannot know which column are lookups (I'm working on a generic library), how could I retrieve those lookups ?
Using the format _lookupName_value allows you to retrieve the lookups:
https://myOrg.api.crm.dynamics.com/api/data/v9.1/contacts(guid)?$select=contactid,fullname,_ownerid_value,_createdby_value
Which of course leaves the problem of knowing which fields are lookups and thus need this formatting.
This can help:
https://myOrg.api.crm.dynamics.com/api/data/v9.1/EntityDefinitions(LogicalName='contact')?$select=LogicalName&$expand=ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)

How to Pass Two Different Json objects and Header bean object to spring mvc controller.?

I have been working with Header and Line Records in Spring MVC.I have header Employee object and Two different Employee Address,Employee Salary details of Json Objects.I need to send all the three objects.
Object 1 - Employee Header Bean object
Object 2- Employee Address Json Object
Object 3- Employee Salary Json Object.
My Question is How to send these objects from Ajax call of jsp and How to receive these objects in the spring mvc controller?
Please help me on this.

Resources