Skip to main content
Ratna_Kumar
Level 10
Jul 14, 2016 5:42 PM UTC
Solved

Calling Status code using JSON Response.

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.
Ratna_Kumar
Ratna_KumarAccepted solution
Level 10
Jul 21, 2016 6:24 PM UTC

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

Level 4
Jul 14, 2016 6:35 PM UTC

Did you hard code the response code into JSON?

Translate
kautuk_sahni
Level 9
Jul 15, 2016 7:17 AM UTC

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

Translate
Kautuk Sahni
Ratna_Kumar
Ratna_KumarAuthorAccepted solution
Level 10
Jul 21, 2016 6:24 PM UTC

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.

Translate