Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return HTTP code 200 from Spring REST API

I want to use this code to receive http link with values:

@PostMapping(value = "/v1/notification")
public String handleNotifications(@RequestParam("notification") String itemid) {
    // parse here the values
    return "result successful result";
}

How I can return http code 200 - successful response?

And also for example if there is a code exception into code processing how can I return error 404?

like image 922
Peter Penzov Avatar asked Aug 07 '18 20:08

Peter Penzov


People also ask

How do I return status codes in Spring rest?

Spring provides a few primary ways to return custom status codes from its Controller classes: using a ResponseEntity. using the @ResponseStatus annotation on exception classes, and. using the @ControllerAdvice and @ExceptionHandler annotations.

What is API response code 200?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.


1 Answers

If you are using spring:

@PostMapping(value = "/v1/notification")
public ResponseEntity handleNotifications(@RequestParam("notification") String itemid) {
    // parse here the values
    return ResponseEntity.ok().build(); 
    //OR ResponseEntity.ok("body goes here");
}
like image 55
Cristiano Bombazar Avatar answered Sep 23 '22 14:09

Cristiano Bombazar