1

I am currently working with the Restlets framework, and I cannot find a way to manually set the HTTP response code within a service method. Consider the following snippet of code:

public class MyResource extends ServerResource {
    @Post("json")
    public Representation doSomething(Representation entity) throws IOException {
        int status = 200;
        try {
            // do something which might throw an exception
        }
        catch (Exception e) {
            // log the exception
            // *** I would like to assign HTTP status 500 here ***
            status = 500;
        }

        JSONObject responseJSON = new JSONObject();
        responseJSON.put("result", "some data");
        Representation rep = new JsonRepresentation(responseJSON.toJSONString());

        return rep;
    }
}

I have the ability to catch and log an exception, should one occur, but it is not clear how I can change the HTTP response code. As far as I know, returning from doSomething will automatically be handled by Restlets with an 200 HTTP response code.

I know how to assign the status code directly from a filter or servlet, but is it possible to do this within Restlets, without going down the servlet layer?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360

1 Answers1

0

As far as I know, there is an object called ResponseEntity which you can use to operate with microservices and a request-response programming model, which allows you to specify the returning HTTP return code. However, you need entities for this, and I think this goes below your abstraction level of Servlets.

You can change them to some predefined values such as HTTP.INTERNAL_SERVER_ERROR and such, which translate to a value in the end, which you can Google in the end.

I hope this was of some help

EDIT:

Import the necessary resource for a ResponseEntity object. In STS, it is

import org.springframework.http.ReponseEntity;
import org.springframework.http.HttpStatus;

public class MyResource extends ServerResource {
@Post("json")
public ResponseEntity<Representation> doSomething(Representation entity) throws IOException {
    int status = 200;
    try {
        // do something which might throw an exception
    }
    catch (Exception e) {
        ResponseEntity<Representation> response = null;
        response = new ResponseEntity<Representation>(HttpStatus.INTERNAL_SERVER_ERROR);
        return response;
    }

    JSONObject responseJSON = new JSONObject();
    responseJSON.put("result", "some data");
    Representation rep = new JsonRepresentation(responseJSON.toJSONString());

    return rep;
}

And sorry for the delay. I am new to Stack Overflow

jasper
  • 123
  • 2
  • 10
  • Can you take my code snippet above and answer in terms of that? You basically said "yes, we can return HTTP codes," but you didn't show us _how_ to do this. – Tim Biegeleisen Dec 29 '17 at 06:15
  • Let me turn on my computer and provide you an example of some work I have done. 30 minutes and you will have it – jasper Dec 29 '17 at 06:17