Search code examples
spring-bootspring-mvcspring-web

Asp.net web api IHttpActionResult springboot Java Equivalent


hi I am new in springboot and I want to develop rest api in springboot. In .net web api IHttpActionresult type used to return entity and httpstatuscode in same time,is there any equivalent in spring boot


Solution

  • Use Spring's Class ResponseEntity<T>. It allows you to add an Object and a Response Status and send to the user. Example from ResponseEntity docs:

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setLocation(location);
    responseHeaders.set("MyResponseHeader", "MyValue");
    return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
    

    You see how ResponseEntity accepts the body of the response as its first argument, the headers of the response as the second and the HTTP status as the third.