Answers for "what is responseentity object in spring boot"

2

sending status code along with entity in spring boot

package com.example.demo.com.example.demo.resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.com.example.demo.model.Employee;
import com.example.demo.com.example.demo.repository.EmployeeRepo;

@RestController
@RequestMapping(value = "/webapi")
public class EmployeeResource {

	@Autowired
	private EmployeeRepo employeeRepo;

	@RequestMapping(method = RequestMethod.GET, value = "/employees")
	public List<Employee> getAllEmployees() {

		return employeeRepo.findAll();
	}

	@RequestMapping(method = RequestMethod.POST, value = "/addemployee")
	public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee) {
		employeeRepo.save(employee);
		System.out.println(employee);
		return new ResponseEntity<Employee>(employee, HttpStatus.CREATED);
	}

	@RequestMapping(method = RequestMethod.GET, value = "/employees/{employeeId}")
	public ResponseEntity<Object> getEmployee(@PathVariable String employeeId) {

		Optional<Employee> employee = employeeRepo.findById(employeeId);

		if (employeeRepo.findById(employeeId).isEmpty()) {
			return ResponseEntity.status(HttpStatus.NO_CONTENT).body(employee);
		} else {
			return ResponseEntity.status(HttpStatus.OK).body(employee);

		}

	}

	@RequestMapping(method = RequestMethod.DELETE, value = "/deleteemployee/{employeeId}")
	public String deleteEmployee(@PathVariable String employeeId) {

		employeeRepo.deleteById(employeeId);
		return "Record Deleted : " + employeeId;

	}

	@RequestMapping(method = RequestMethod.PUT, value = "/updateemployee")
	public ResponseEntity<Object> updateemployee(@RequestBody Employee employee) {

		if (employeeRepo.existsById(employee.getEmployeeId())) {
			employeeRepo.save(employee);
			return ResponseEntity.status(HttpStatus.ACCEPTED).body(employee);
		} else {

			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(employee);
		}

	}

}
Posted by: Guest on July-02-2020
0

responseentity object

<?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
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>responseentityex</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>
Posted by: Guest on September-17-2020
0

responseentity object

package com.zetcode.model;

public class Country {
    
    private String name;
    private int population;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }
}
Posted by: Guest on September-17-2020

Code answers related to "what is responseentity object in spring boot"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language