MIME stands for Multipurpose Internet mail-extension.
MIME describes the response format sends by server to the client.
Syntax:
type/sub-type
Ex:text/html,application/json etc.
MIME stands for Multipurpose Internet mail-extension.
MIME describes the response format sends by server to the client.
Syntax:
type/sub-type
Ex:text/html,application/json etc.
A http request has 3 parts-
1.Request line-The http command(Ex-get,post,put,delete)
2.Header variable-Request meta data(Ex-connection ,keep alive etc)
3.Message body-Content of the message(payload)
A http response has also 3 different areas-
1.Response line-Server protocol and status code
2.Header variable-response metadata(ex:content type-json,xml,size and length of the data etc)
3.Message body(list of customer as json/xml)
By default jackson use appropriate getter and setter method to bind data .
lets see and example ,
Json to pojo--
let's see a practical example-
we have a file containing a json object representing student . Then read json object from the file and convert it to student object.
.import java.io.File;
.import com.fasterxml.jackson.databind.ObjectMapper;
public class MainApp{
public static void main(String[] args){
ObjectMapper objectMapper = new ObjectMapper();
Student student = objectMapper.readValue(new File("sample.json"),Student.class);
}
}
In this example objectMapper will read value from sample.json file and convert that json object into student object using corresponding setter methods.
N.B:When we converting json to pojo it will call setter but in vice versa it will call getter .But what if the corresponding getter and setter are not found then it will throw exception . So how we can avoid some property if needed .Then we can use special annotation -
@JsonIgnoreProperties(ignoreUnknown=true)
public class Student{
private int id;//has setter and getter
private String firstName;//has setter and getter
private String lastName;
}
Data binding is the process of converting json to java pojo or java pojo to json .
spring uses jackson project for data binding .
-jackson is data binding API.
package: com.fasterxml.jackson.databind
Maven dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
JSON- Javascript object notation.
-- Lightweight data format for storing and exchanging data(as plain text).
--Language independent.
JSON object example-
{
"id":1,
"name":"Rafik",
"age":20
}
Object members are name value pair. Name and value are delimited by colon(:)
N.B: Name are always in double quote("").
Nested JSON object
{
"id":1,
"name":"Sadia siddika muna",
age:24,
department:{
"name":"english",
"department code":101
}
}
JSON arrays:
{
"id":101,
"name":"muna",
"address":"Barishal",
"languages":["java","python","javascript"]
}
Let's see a simple JSON array example having 3 objects.
{
"employees":[
{"name":"muna","email":"muna@gmail.com","age":24},
{"name":"sadia","email":"sadia@gmail.com","age":24},
{"name":"siddika","email":"siddika@gmail.com","age":24},
]
}
We should add those below dependencies to pom.xml-
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${springsecurity.version}</version>
</dependency>
------Controller------------- @RestController @RequestMapping("/items") public class ItemController { private final ItemServic...