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.

6
  • Possible duplicate of How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String? Commented Apr 1, 2018 at 18:59
  • I need to send both method and status when the function is done. Commented Apr 1, 2018 at 19:00
  • "Send method": What do you mean by that? Commented Apr 1, 2018 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 Commented Apr 1, 2018 at 19:05
  • When you respond you can return only Http Status Code in which case 201 Created for your POST Request. Commented Apr 1, 2018 at 19:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.