PostulanteController.java 1.63 KB
Newer Older
1 2 3
package com.roshka.controller;


4
import javax.validation.ConstraintViolationException;
5

6

7 8 9 10
import com.roshka.modelo.Postulante;
import com.roshka.repositorio.PostulanteRepository;

import org.springframework.beans.factory.annotation.Autowired;
11 12
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Controller;
14 15
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
16 17 18 19 20 21


@Controller
public class PostulanteController {
    @Autowired
    PostulanteRepository post;  
22

23
    @RequestMapping("/")
24 25
    public String index() {
        return "index";
26 27
    }

28 29
    @RequestMapping("/postulante")
    public String getFormPostulante(){
30
        
31 32
        return "postulante-form";
    }
33

34 35
    @PostMapping(value = "/postulante",consumes = "application/json")
    public String guardarPostulante(@RequestBody Postulante postulante){
36
        post.save(postulante);
37
        return "redirect:/";
38 39
    }

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseEntity<String> handleValidationExceptions(
            MethodArgumentNotValidException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ex.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({ConstraintViolationException.class})
    public ResponseEntity<String> handleValidationExceptions2(
            ConstraintViolationException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ex.getMessage());
    }

56
}