Answers for "spring boot validate api requests"

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

spring valid request body custom message

@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

        @Override
        protected ResponseEntity<Object> handleMethodArgumentNotValid(
                MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

           // ex.getBindingResult(): extract the bind result for default message. 
              String errorResult = ex.getBindingResult().toString();
             CustomizedExceptionHandlerResponse exceptionResponse = new CustomizedExceptionHandlerResponse(
                    errorResult, new Date(), request.getDescription(false));

            return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
        }


}

class CustomizedExceptionHandlerResponse {

   private String message;
   private String status;
   private Date timestamp;

   // constuctor, setters, getters...
}
Posted by: Guest on May-09-2020

Code answers related to "spring boot validate api requests"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language