sending status code along with entity in spring boot

Solutions on MaxInterview for sending status code along with entity in spring boot by the best coders in the world

showing results for - "sending status code along with entity in spring boot"
Louison
31 Feb 2018
1package com.example.demo.com.example.demo.resource;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Optional;
6
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.http.HttpStatus;
9import org.springframework.http.ResponseEntity;
10import org.springframework.web.bind.annotation.PathVariable;
11import org.springframework.web.bind.annotation.RequestBody;
12import org.springframework.web.bind.annotation.RequestMapping;
13import org.springframework.web.bind.annotation.RequestMethod;
14import org.springframework.web.bind.annotation.RestController;
15
16import com.example.demo.com.example.demo.model.Employee;
17import com.example.demo.com.example.demo.repository.EmployeeRepo;
18
19@RestController
20@RequestMapping(value = "/webapi")
21public class EmployeeResource {
22
23	@Autowired
24	private EmployeeRepo employeeRepo;
25
26	@RequestMapping(method = RequestMethod.GET, value = "/employees")
27	public List<Employee> getAllEmployees() {
28
29		return employeeRepo.findAll();
30	}
31
32	@RequestMapping(method = RequestMethod.POST, value = "/addemployee")
33	public ResponseEntity<Employee> addEmployee(@RequestBody Employee employee) {
34		employeeRepo.save(employee);
35		System.out.println(employee);
36		return new ResponseEntity<Employee>(employee, HttpStatus.CREATED);
37	}
38
39	@RequestMapping(method = RequestMethod.GET, value = "/employees/{employeeId}")
40	public ResponseEntity<Object> getEmployee(@PathVariable String employeeId) {
41
42		Optional<Employee> employee = employeeRepo.findById(employeeId);
43
44		if (employeeRepo.findById(employeeId).isEmpty()) {
45			return ResponseEntity.status(HttpStatus.NO_CONTENT).body(employee);
46		} else {
47			return ResponseEntity.status(HttpStatus.OK).body(employee);
48
49		}
50
51	}
52
53	@RequestMapping(method = RequestMethod.DELETE, value = "/deleteemployee/{employeeId}")
54	public String deleteEmployee(@PathVariable String employeeId) {
55
56		employeeRepo.deleteById(employeeId);
57		return "Record Deleted : " + employeeId;
58
59	}
60
61	@RequestMapping(method = RequestMethod.PUT, value = "/updateemployee")
62	public ResponseEntity<Object> updateemployee(@RequestBody Employee employee) {
63
64		if (employeeRepo.existsById(employee.getEmployeeId())) {
65			employeeRepo.save(employee);
66			return ResponseEntity.status(HttpStatus.ACCEPTED).body(employee);
67		} else {
68
69			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(employee);
70		}
71
72	}
73
74}
75
queries leading to this page
spring boot controller return custom http status codechange status sent in spring controllerreturn response with status code spring bootjava spring boot http request status codesending status in springbootspring boot responseentity status codejava spring return custom http status codespringboot controller returning custom statusspring response set isokayget status 201 in springbootsending status in spring boot apiresponseentity 3cobject 3e returning mapcustom responseentity statushow to send status code in spring bootspring status code responsespring mvc return custom responsehow to set resposecodes in springbootwhen to use responseentity in spring bootspring return http codespring boot return custom http codesending status codes in spring data jpasend response back in spring bootstatus spring bootspring change request response codehow to send status codes in spring bootreturn http response in controller spring bootspring mvc return status codehow to implement to status in one method of controller classhow to return only http response spring bootspring return status code with messagespring boot return status texthow to send http status code in spring boothow to set response status code in springjava spring status code of urlhow to set status code in responseentityspring boot set return codesending status code along with entity in spring bootreturn response code spring bootspring api with response status codes in spring boot apispring boot return wih status codehttp response spring boothow to return data with http status code in spring boothow to return right status code in spring bootspring boot return non standard http codespringboot send request statussending status and message with spring with conditionrestcontroller custom status codehow set status code in spring boot resthttpstatus specific code with spring bootreturn 201 resource code spring bootspring rest controller return 200how to use responseentity in spring bootadd status code from java spring bootwhat is responseentity object in spring bootjava spring boot response 200set statuscode in responseentity javasending status and message with springspring return response codespring boot rest api return status codespring boot controller http responsespring mvc return http statusresponseentity status code 200how to return 201 response in spring boothwo to pass http status code in responseentityspring boot set response statusjava return status codehow to return http status code in spring boothow to set status code in http response springspring boot responseentity or void in rest apispring controller return http status codespring hhow send http code 2b201how to get status code from api response in spring bootspringboot annotation to send status codehow to return status code in spring bootspring postmapping return different status codehow to return status ok in rest api spring bootspring controller return custom http status codespring return custom status code controllerresponseentity change status code in services spring bootspring boot set request status codejpa httpstatus okhow to set http status while returning responseset boot custom statusspring change status codepostmapping return reponssending status in spring boothow to get status code from response in spring bootcustom response code spring bootspring mvc return response codespring return status codehow to give response status in java springhow to send respnse as status code in springspring rest controller statusspring status code examplesspring send status as responseresponseentity with status code and messagespring boot validate api requestsput rest controller set response success status headerjava spring send http status coderesponseentity in spring bootset response header ok status spring bootspring boot return response with codehow to send http response code in spring bootspring boot post return status codespring boot return status codereturn http ok from spring boot controllerspring boot create custom responsehow to set status in response spring bootspring send http response codesending status code along with entity in spring boot