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.
class BugResponse { private List<InsectObject> bugs; getters/setters .. }and use it as the return ofBugSiteController.getAll()