Answers for "when to use responseentity 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

package com.zetcode.controller;

import com.zetcode.bean.Country;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    
    @RequestMapping(value = "/getCountry")
    public ResponseEntity<Country> getCountry() {
        
        var c = new Country();
        c.setName("France");
        c.setPopulation(66984000);
        
        var headers = new HttpHeaders();
        headers.add("Responded", "MyController");
        
        return ResponseEntity.accepted().headers(headers).body(c);
    }
    
    @RequestMapping(value = "/getCountry2")
    @ResponseBody
    public Country getCountry2() {
        
        var c = new Country();
        c.setName("France");
        c.setPopulation(66984000);
        
        return c;
    }    
}
Posted by: Guest on September-17-2020

Code answers related to "when to use responseentity in spring boot"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language