Vahid Hashemi
Vahid Hashemi

Reputation: 5240

Return HTTP response with status code 202 in Spring Boot

How can I return HTTP response with status code 202?

I couldn't find any reference for it in Spring Boot documentation.

Upvotes: 2

Views: 11238

Answers (3)

ynerdy
ynerdy

Reputation: 604

You can do like this (I am using kotlin) :

return ResponseEntity.accepted().body(OrderResponse().order(order))

OR Easy way is:

    return ResponseEntity.status(HttpStatus.CREATED).body(OrderResponse().order(order))

ResponseEntity.accepted returns 202 ResponseEntity.created returns 201 and so on

Upvotes: 2

Sibsankar Bera
Sibsankar Bera

Reputation: 1

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

@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No Article found with this Article Number.")
public class NoArticleFoundException extends RuntimeException{

    private static final long serialVersionUID =3935230281455340039L;
}

Instead of writing NOT_FOUND , you can write ACCEPTED and give some text in reason field. And then call this method from another class or when ever required. (extends part is not required)

Upvotes: -1

Ori Marko
Ori Marko

Reputation: 58862

Return HTTPStatus ACCEPTED, e.g:

 return new ResponseEntity<>(HttpStatus.ACCEPTED);

202 Accepted.

Upvotes: 8

Related Questions