jabrena / SpringCloudLab

SpringCloud examples
MIT License
0 stars 1 forks source link

Granularity in REST output #46

Open jabrena opened 8 years ago

jabrena commented 8 years ago
    @RequestMapping(
            value = "route2", 
            method = RequestMethod.POST, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> route2(
            @RequestHeader(value="Content-Type") String contentType, 
            @Valid @RequestBody ModelBasic car,
            BindingResult bindingResults) {

        if (bindingResults.hasErrors()) {
            Map map = new HashMap();
            map.put("error", "bad value bindingResults");
            return new ResponseEntity<Map>(map, HttpStatus.BAD_REQUEST);    
        }

        System.out.println(contentType);
        System.out.println(car.key1);
        if(!car.key1.equals("value1")){
            Map map = new HashMap();
            map.put("error", "bad value on POJO");
            return new ResponseEntity<Map>(map, HttpStatus.BAD_REQUEST);                    
        }

        Map map = new HashMap();
        map.put("prop1", "value1");
        map.put("prop2", "value2");

        return new ResponseEntity<Map>(map, HttpStatus.OK); 
    }
jabrena commented 8 years ago

https://www.jayway.com/2014/10/19/spring-boot-error-responses/

@ExceptionHandler(IllegalArgumentException.class)
void handleBadRequests(HttpServletResponse response) throws IOException {
    response.sendError(HttpStatus.BAD_REQUEST.value(), "Please try again and with a non empty string as 'name'");
}
jabrena commented 8 years ago

http://briansjavablog.blogspot.co.uk/2015/12/spring-boot-rest-tutorial.html

package com.blog.samples.boot.rest.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@ControllerAdvice
public class ControllerExceptionHandler {

    @ResponseStatus(HttpStatus.NOT_FOUND) // 404
    @ExceptionHandler(CustomerNotFoundException.class)
    public void handleNotFound() {
        log.error("Resource not found");
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST) // 400
    @ExceptionHandler(InvalidCustomerRequestException.class)
    public void handleBadRequest() {
        log.error("Invalid Fund Request");
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500
    @ExceptionHandler(Exception.class)
    public void handleGeneralError(Exception ex) {
        log.error("An error occurred procesing request", ex);
    }
}
jabrena commented 8 years ago

http://stackoverflow.com/questions/30388169/spring-boot-starter-web-controlleradvice-annotation-is-not-work-without-having

@ControllerAdvice
public class GlobalDefaultExceptionHandler {

@ExceptionHandler(value = RuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestEntity handleException(HttpServletRequest req, RuntimeException ex) {
    RestEntity restEntity=new RestEntity();
    Message message=new Message();
    message.setCode(1000);
    message.setMessage("Something wrong with the server");
    restEntity.setMessage(message);
    return restEntity;
}
}
jabrena commented 8 years ago

http://mvc-exceptions-v2.cfapps.io/ https://github.com/paulc4/mvc-exceptions/blob/master/src/main/java/demo1/web/ExceptionHandlingController.java

jabrena commented 8 years ago

http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/

jabrena commented 8 years ago

http://stackoverflow.com/questions/23580509/how-to-write-a-proper-global-error-handler-with-spring-mvc-spring-boot

jabrena commented 8 years ago

http://stackoverflow.com/questions/23582534/how-to-handle-exceptions-in-spring-mvc-differently-for-html-and-json-requests

jabrena commented 8 years ago

https://github.com/gitekiras/spring-boot-exception-handling

jabrena commented 8 years ago

http://blog.didispace.com/springbootexception/