0

So I'm working on building a lil website that's supposed to showcase insects and some info about em, and I've gotten the fetch call and the site itself up and running, no problem. On the back-end I have an API with a Spring Boot that retrieves my InsectObject, which for now just holds a title and description string and that works fine.

Now, excuse me as I try to explain the issue to the best of my abilities.

My problem is that I'm getting a response as follows from my API:

[
    {
        "id": 1,
        "title": "mantis",
        "description": "leafy boi"
    },
    {
        "id": 2,
        "title": "moth",
        "description": "fly boi"
    }
]

Wheres I want it to return it as:

{
bugs: [
    {
        "id": 1,
        "title": "mantis",
        "description": "leafy boi"
    },
    {
        "id": 2,
        "title": "moth",
        "description": "fly boi"
    }
  ]
}

Which is how I think a proper api call should look like. But then again, it's the first time I venture into this territory and I've only been following tutorials and documentation, building my own picture along the way.

If it's of any relevance, my rest controller looks like this:

@RestController
public class BugSiteController {

    private final InsectRepository repository;

    BugSiteController(InsectRepository repository) {
        this.repository = repository;
    }

    // get all bugs from the repo
    @CrossOrigin(origins = "http://localhost:3000")
    @GetMapping("/bugs")
    List<InsectObject> getAll() { 

        return repository.findAll();
    }
}

What am I missing? Is it something in my getAll() method I should change to get the desired result? or should I be able to work with the first result regardless? Should I maybe return something other than a List<>? I tried with ResponseEntity but had the exact same result, just way more verbose.

Thanks in advance.

1
  • You can create a class wrapper for a response like: class BugResponse { private List<InsectObject> bugs; getters/setters .. } and use it as the return of BugSiteController.getAll() Commented Jun 10, 2019 at 8:56

2 Answers 2

1

You could set the value in Model or you could set it in a Map.

Model

class InsectResponse {

  @JsonProperty("bug")
  private List<InsectObject> insectObject;
  // Getter, Setter & Constructor
}

Controller

@GetMapping("/bugs")
public ReponseEntity getAll() { 
   return ResponseEntity.ok(new InsectResponse(repository.findAll()));
}

or

@GetMapping("/bugs")
public ReponseEntity getAll() { 
   return ResponseEntity.ok(new HashMap<String, List<InsectObject>>() {{
            put("bug", repository.findAll());
        }});
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, using a Map worked perfectly, and it was less stuff to implement (I like keeping my codes small). May I ask what's going on in the ResponseEntity.ok() method? I haven't encountered double {{}}} brackets and I don't understand why the put() method is in there and fully functional.
I have initialized the Map anonymously, instead of creating an object and calling a put method. You could check javatutorial.net/java-hashmap-inline-initialization it will give you clear understanding
1

On the InsectObject entity class add:

@JsonRootName(value = "bug")

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.