1

I'm having an issue getting a value while using a ResponseEntity. In simplest terms, after making an API call and using

ResponseEntity<String> response = *api call*
String value = response.getBody();

I get as an output

{"value":123456}

However I would like to have the string: value be equal to just 123456 without simply brute forcing it. Any Ideas on how to do so?

1
  • 1
    Don't ask for a ResponseEntity<String>. Ask for a ResponseEntity<YourClassMappingThisJsonObject>. If youposted your code, it would be much easier to explain how to modify it. Commented Oct 15, 2018 at 16:13

2 Answers 2

2

Don't use string as the generic of the ResponseEntity object. Create your own object like this:

public class MyResponse {
    private String value;

    public String getValue () {
        return this.value;
    }

    public void setValue (String value) {
        this.value = value;
    }
}

And then use this object like this:

ResponseEntity<MyResponse> response = *api call*
String value = response.getBody().getValue();
Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest this approach
0

The behaviour is normal since your ResponeEntity is wrapping a String.

You should rather use a class that defines an attribute value.

Assuming you have a class MyClass that defines the attribute value (with proper getter), all you have to do is to change your code as follows :

ResponseEntity<MyClass> response = *api call* 

String value = response.getBody().getValue();

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.