how to send json response from spring boot controller

Directory

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>SpringBootRestJsonResponse</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootRestJsonResponse</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Employee.java

package com.example.demo.entity;

import java.util.List;
import java.util.Map;
import java.util.Set;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    private Integer eno;
    private String ename;
    private String dept;
    private Float sal;
    private List<String> proj;
    private Set<String> Companies;
    private Map<String, String> cards;
    private Address add;
}

Address.java

package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
    private Integer houseNo;
    private String street;
}

EmployeeController.java

package com.example.demo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.Address;
import com.example.demo.entity.Employee;

@RestController
public class EmployeeController {

    @GetMapping("/display")
    public ResponseEntity<Employee> getData(){
        //list of project
        List<String> list = Arrays.asList("proj1","proj2","proj3");
        //companies
        Set<String> set = Set.of("abc","def");
        //card Details
        Map<String,String> map = Map.of("debit","12AB","credit","123DAB");
        //address
        Address add = new Address(123,"SEO");
        //emp obj
        Employee emp = new Employee(102,"Carla","DEV",5000.0f,list,set,map,add);
        return new ResponseEntity<>(emp,HttpStatus.OK);
    }
}

Run the app

Open postman type url with get mode request

Response

Did you find this article valuable?

Support realNameHidden by becoming a sponsor. Any amount is appreciated!