How to pass Multiple Data Types from Angular to Spring RESTful web services
Tags: java, angular, api, spring
Published: 2022-09-20 20:20:39



## Client side : 

To communicate with a server over HTTP. Angular makes requests and receive responses using HttpClient and the data comes in the format of JSON by default.

JSON (JavaScript Object Notation) is a format for data sent to and from APIs. 


Consider we have a JavaScript object **student** and a number **idD** for example.
We want to send both the object and the number to the server side.

To do this, first create an object with these 2 elements 

 `{student: student, idD: idD}`

Now convert the object into a JSON string using `JSON.stringify()`

Our code is going to look like this:


```
public saveStudent(student : Student, idD:number):Observable<Object>{
    var data=JSON.stringify({student: student, idD: idD})
    return this.http.post<Object>(environment.backendHost+"/admin/students" ,data );
  }

```

Now your data is going to be sent to the server as a string.



## Server side: 


First Add the following dependencies to your POM.XML file:


```
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20220320</version>
        </dependency>
```



```
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.9.1</version>
        </dependency>
```



Remember that we sent a String from the client side to the server side.
 
Your controller class should be expecting a String 

```
@PostMapping(value = "/admin/students")
    public Student saveStudent(@RequestBody String myObject ) {
```

To convert the String to a JSON Object, we will be using **JSONObject** which is a subclass of **java.util.HashMap.** 



First we need an implementation of a JSON package in Java.

Github repository:

{% embed https://github.com/stleary/JSON-java %}


Import the following in your Rest Controller:


```
import org.json.JSONObject;

```

Inside your controller pass you object as a parameter to the JSONObject's constructor

```
JSONObject jsonObject = new JSONObject(myObject);

```



In this example we're going to be using two JSONObject methods:
`getJSONObject(String name)
`
That returns the object value to which the specified name is mapped.
And 

`getInt(String name)
`
That returns an integer from JSON number.




```
JSONObject myobj = jsonObject.getJSONObject("student");
int idD= jsonObject.getInt("idD");

```
The last step is to transform JSON object into a Java Object 

I'm going to be using Gson which is a Java library that is used to serialize and deserialize Java objects to JSON

> Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

Github repository:
{% embed https://github.com/google/gson %}

Create a Gson object using the constructor:

```
Gson gson= new Gson();

```

In this example we're going to be using the fromJson() method

```
fromJson(java.lang.String json, java.lang.reflect.Type typeOfT)

```
This method deserializes the specified Json into an object of the specified type.

Convert your **JSONObject** **myobj** into a string using `toString() `

Then pass it as the first parameter to the fromJson() method.
The second parameter is going to be your java class.

```
Student student = gson.fromJson(myobj.toString(),Student.class);
        
```