Alexandre Bento
Alexandre Bento

Reputation: 35

Seting HTTP Response Code and HTTP Method

I have that code

@RequestMapping(value = "/graph", method = RequestMethod.POST)
@ResponseBody
public HttpServletResponse graphImport() {

    JSONParser parser = new JSONParser();
    GraphJson savedGraph = new GraphJson();

    try {

        Object obj = parser.parse(new FileReader("graph.json"));

        JSONObject jsonObject = (JSONObject) obj;

        GraphJson graph = new GraphJson();
        graph.setSource(jsonObject.toString());
        session.save(graph);

        savedGraph = session.get(GraphJson.class, 1);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    System.out.println("CREATED");
    return resp;
    //return "id :" + savedGraph.getId() + ", " + savedGraph.getSource();
}

And when I use the

curl http://localhost:8080/graph

When I sent one POST request to that URL I need to return status 201 for created and method POST. Need some help on that. Thank you.

Upvotes: 1

Views: 91

Answers (1)

Alexandre Bento
Alexandre Bento

Reputation: 35

I used

@ResponseStatus(HttpStatus.CREATED)

between the @RequestMapping and @ResponseCode and it worked when I sent one post request it returned

code 201 CREATED

Remembering that the CREATED after the HttpStatus. could be another code.

Upvotes: 1

Related Questions