Tuco
Tuco

Reputation: 1052

Creating Java Spring Boot endpoint to obtain list of objects as json

I'm trying to create an endpoint that returns a list of objects as a JSON.

The current object structure is as follows:

units: [
        {
         id: #,
         order: #,
         name: ""
         concepts: [
                    {
                     id: #
                     name: ""
                    },
                    ...
         ]
        },
        ...
]

A list of units which have 4 attributes, one of them being another list of objects with 2 otehr attributes. This is kind of the result I'm looking to obtain.

I was currently trying to do the following in my UnitController:

@RequestMapping(method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
public @ResponseBody List<Unit> getUnits() {
    return unitService.findAll();
}

But whenever I run the app and try curl localhost:8080/units, I get nothing. Could this be because of another method I have in the controller, like this one:

@RequestMapping("")
public String index(Map<String, Object> model) {
    List<Unit> units = unitService.findAll();
    model.put("units", units);
    return "units/index";
}

Could someone kindly help me through this and tell me what I'm doing wrong? I'd really appreciate it.

EDIT

Ok so I moved the annotation on top of the class

@Controller
@RequestMapping(value = "/units", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
public class UnitController extends BaseController {
...
}

And tried the endpoint like this:

@RequestMapping(method = RequestMethod.GET, value = "/units.json")
public @ResponseBody List<Unit> getUnits() {
    return unitService.findAll();
}

But it curl localhost:8080/units.json is still giving me no response.

Also forgot to mention that my application.properties file doesn't have a server.contextPath property.

Upvotes: 1

Views: 7345

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30839

That might be because of no @RequestMapping annotation on controller. Spring boot needs mapping to figure out which method needs to be called when a REST API request is sent. E.g. You need the following on UnitController class:

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, value = "/units")
public class UnitController {

If your controller class already has that, then you need to define another mapping for methods, specifying request method and optionally, mapping. E.g.

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, value = "/units")
public class UnitController {

   @RequestMapping(method = RequestMethod.GET)
   public List<Unit> method1(){..}

   @RequestMapping(method = RequestMethod.POST)
   public List<Unit> method2(){..}

   @RequestMapping(method = RequestMethod.GET, value = "/unit")
   public List<Unit> method3(){..}
}

For the above example:

  • Sending a GET request to /units will result in invocation of method1
  • Sending a POST request to /units will result in invocation of method2
  • Sending a GET request to /units/unit will result in invocation of method3

If your application.properties file has server.contextPath property defined then it needs to be appended to the baseurl, e.g. <host>:<port>/<contextPath>

Upvotes: 2

Related Questions