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

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.

Related

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

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.

How to handle multiple request params in spring boot?

Suppose I have a Employee class. It has got many fields like id, firstName, lastName, designaton, age, salary and other fields too. Now I am making a Get Query where I want to use all of these fields (required=false) to be passed as Request Params.
But the question is, there could be many combinations like (firstName,age) or (age,salary,lastName) or (designation,age,salary,lastName) and many more like this. So how should I handle all this filters. Shall I have to write each query for each case?
PS: I am using Spring Boot with Spring Data Jpa.
For this you will have to send Object from where you can get your combination. There may be many combination. So from this perspective you will send value as object and for database query you will select your combinations from that object.
If you want different combination, it won't be a good practice to write controller for every combination. So you can send a Object instead of RequestParam value where you can get your combinations from the Object
Example :
Class Employee{
// Your class instance variable
// Which is called your combinations
}
public Employee getEmployeeByName(Employee employee){
// now you send your desired combination from employee class for
// database query
}
Where you have too many fields, it's not a good practice to send all fields as RequestParam. Think of your class getting bigger day by day and you editing the controller method continously.
Better way is send as a object. No need to edit controller later. Only change the entity class

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

Save multiple row data and some other data into two tables simultaneously in spring hibernate

I want to save data of a form into two tables simultaneously. The form contains multiple identical rows with some fields like
item, unit, unit_price, total.
1.) In the table1 I would like to save some data like id(auto generated, primary key), date, creator.
2.) In table2 I would like to save the id(auto generated, primary key), fid(id of the table1, foreign key), item, unit, unit price, total. Remember, in table2 multiple rows will be save from the form data.
I can't figure it out. Please help me with a valid step by step example, and please don't use static or pre-defined data, take the data from form only.
Is your form an HTML form on a web page, and are you writing a Spring MVC application? Have you setup Spring Security, or some other authentication mechanism such that you know who the user is? If so, here is one approach:
Define a plain old Java object (POJO) with fields id, unit, unit-price, total, creator, currentDate; perhaps you would name it Item.
Define another plain old Java object perhaps named "ItemHistory", with its own ID and a reference to an Item.
In the first POJO ("Item") include a reference to a list of ItemHistorys.
Annotate both classes with Hibernate/JPA annotations, such that the Item has a OneToMany relationship to ItemHistory.
Define a Spring MVC controller with a method annotated as a POST method, with an Item as one parameter, and an Authentication as another parameter (supposing you have configured Spring Security, the Authentication will automatically be populated with the identity of the currently-logged-in user. In this method, set the currentDate and author of the Item to the current date and the principal from the Authentication parameter. If it is a new Item (no id yet), add the first ItemHistory as a copy of the Item itself. If it is an existing item, add another ItemHistory if needed.
Define a Spring bean, perhaps named ItemService, with a method annotated as transactional, that takes an Item and saves it. Call this bean from the REST controller. Such a Spring bean is known as a DAO (data access object).
There are other strategies, but if you already have Spring MVC, Spring Security, and Hibernate/JPA configured to your needs, this one is pretty straight-forward.

MVC: Is it considered bad form to give a DTO a reference to the data access layer?

Is it considered bad form to give a DTO a reference to the data access layer?
Or should you always pass a DTO between the data access layer and the application layer?
EDIT: For example, imagine the following:
I keep a product types list in my database.
I'd like to render this list in a drop-down box in a partial view.
This partial view is strongly-typed to a DTO.
Question:
Should I retrieve my product types list first, and then pass it to the DTO via its constructor?
Or is it acceptable to pass a repository reference to the DTO, and then expect it to retrieve this list from the data access layer?
A DTO should never have a reference to the data access layer.
Rather a DTo is a simple transfer object that contains only data and is used to pass information between layers.
A DTO is for passing data from the business layer to your presentation layer. This way you can bind the DTO to your combobox.
The DTO should be populated inside the business layer (middle tier), like when calling a service. The service will call the DAL by for example by DAO's.

Resources