Calling Status code using JSON Response. | Community
Skip to main content
Ratna_Kumar
New Participant
July 14, 2016
Solved

Calling Status code using JSON Response.

  • July 14, 2016
  • 3 replies
  • 1464 views

Hi,

We hard-coded the Status codes and messages like ex: 200(Success), 403(Forbidden), 404(Nt found). etc., Now we have to call the Status codes through JSON response.

Can anyone tell me how to do this and any code snippet will be helpful to me.

Thanks,
Ratna Kumar.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ratna_Kumar

Ratna Kumar wrote...

Hi,

We hard-coded the Status codes and messages like ex: 200(Success), 403(Forbidden), 404(Nt found). etc., Now we have to call the Status codes through JSON response.

Can anyone tell me how to do this and any code snippet will be helpful to me.

Thanks,
Ratna Kumar.

 

The issue has been resolved.

3 replies

Ratna_Kumar
Ratna_KumarAuthorAccepted solution
New Participant
July 21, 2016

Ratna Kumar wrote...

Hi,

We hard-coded the Status codes and messages like ex: 200(Success), 403(Forbidden), 404(Nt found). etc., Now we have to call the Status codes through JSON response.

Can anyone tell me how to do this and any code snippet will be helpful to me.

Thanks,
Ratna Kumar.

 

The issue has been resolved.

kautuk_sahni
Employee
July 15, 2016

Hi Ratna,

I am not very clear what exactly you mean by "We hard-coded the Status codes and messages " and "to call the Status codes through JSON response."

If the question is how to extract HTTP Status code, then Use the RestTemplate#exchange(..) methods that return a ResponseEntity. This gives you access to the status line and headers (and the body obviously).

Or Use HttpURLConnection:

//

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();

 

Add the status code to Json before returning. 

Reference Article:- http://stackoverflow.com/questions/4687271/jax-rs-how-to-return-json-and-http-status-code-together

I hope this will help you.

 

Thanks and Regards

Kautuk Sahni

Kautuk Sahni
New Participant
July 14, 2016

Did you hard code the response code into JSON?