0

I am using Spring boot and I need to pass JSON list in @RestController using ResponseEntity:

@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity getCustomers() {

        List<Customer> customerJsonList = new ArrayList();
        List<CustomerTable> customerList = (List<CustomerTable>) customerRepository.findAll();

        for(CustomerTable customerTable : customerList) {
            Customer customer = new Customer();
            customer.setId(customerTable.getId());
            customer.setFirstname(customerTable.getFirstname());
            customer.setLastname(customerTable.getLastname());
            customer.setAddress(customerTable.getAddress());
            customerJsonList.add(customer);
        }

        return ResponseEntity.ok(customerJsonList);
    }

When I tried to test in postman I get an empty body. When I tried in Chrome browser I am getting:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 11 10:19:54 AEDT 2017 There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
WPS
  • 21
  • 1
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/14251851/what-is-406-not-acceptable-response-in-http – gajju_15 Feb 16 '18 at 06:45

4 Answers4

0

As far as I know, you should specify a mapping value, try something like :

@RequestMapping(value = "mymappingurl", method = RequestMethod.GET)

After that, try your url with "mymappingurl" appended to it. Right now, the servlet container goes to fallback url host/error for which you don't have anything configured either, hence you see this response.

mnj11
  • 111
  • 3
0

After adding

    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
    </dependency>

It is working fine

WPS
  • 21
  • 1
  • 6
0

Have your Method return ResponseEntity<?> getCustomers()

Then Rerun below:

return new ResponseEntity<List<Customer>>(customerJsonList,HttpStatus.OK);

kj007
  • 6,073
  • 4
  • 29
  • 47
0
@RequestMapping(value = "", method = RequestMethod.GET)
public List<Customer> getCustomers() {

        List<Customer> customerJsonList = new ArrayList();
        List<CustomerTable> customerList = (List<CustomerTable>) customerRepository.findAll();

        for(CustomerTable customerTable : customerList) {
            Customer customer = new Customer();
            customer.setId(customerTable.getId());
            customer.setFirstname(customerTable.getFirstname());
            customer.setLastname(customerTable.getLastname());
            customer.setAddress(customerTable.getAddress());
            customerJsonList.add(customer);
        }

        return customerJsonList;
    }
vimuth
  • 5,064
  • 33
  • 79
  • 116
tharun
  • 1
  • Welcome to Stack Overflow! Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – Rallen Jun 03 '19 at 06:51