1

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.

Alexandre Bento
  • 35
  • 1
  • 10
  • Possible duplicate of [How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?](https://stackoverflow.com/questions/16232833/how-to-respond-with-http-400-error-in-a-spring-mvc-responsebody-method-returnin) –  Apr 01 '18 at 18:59
  • I need to send both method and status when the function is done. – Alexandre Bento Apr 01 '18 at 19:00
  • "Send method": What do you mean by that? –  Apr 01 '18 at 19:02
  • I did what that link suggests and got this "There was an unexpected error (type=Not Found, status=404)". I don't know how to say that, but when I use curl it give me HTTP/a.a 200, i need the 201 which is CREATED and HTTP METHOD POST, but it didnt show that, and by the way, as my code shows on RequestMethod.POST I got an error saying that can't use GET method... i'm really confuse – Alexandre Bento Apr 01 '18 at 19:05
  • When you respond you can return only Http Status Code in which case 201 Created for your POST Request. – royalghost Apr 01 '18 at 19:11
  • I used @ResponseStatus(HttpStatus.CREATED) and worked – Alexandre Bento Apr 02 '18 at 12:58

1 Answers1

1

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.

Alexandre Bento
  • 35
  • 1
  • 10