0

I am trying to send a json long list and take records from db.

My controller is:

@Api(tags = Endpoint.RESOURCE_customer, description = "customer Resource")
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public class CustomerResourceController {

    private final customerService customerService;


    public CustomerResourceController(customerService customerService) {

        this.customerService = customerService;
    }

    @ApiOperation(
            value = "Return customer",
            response = customerDto.class, responseContainer="List"
    )
    @PostMapping(value = Endpoint.RRESOURCE_customer_ID)
    public List<customerDto> getCustomersByIds(@RequestBody List<Long> ids) {
        return customerService.findcustomerIds(ids);
    }

}

and client class is:

@Headers("Content-Type: " + MediaType.APPLICATION_JSON_VALUE)
public interface CustomerClient {

@RequestLine("POST /customer/customers/search")
    List<LocGrpDto> getCustomersByIds(@RequestBody @Validated List<Long> ids);
}

And i test this service in postman with JSON:

{ "ids": [1,7,8] }

But I get this error:

{
    "timestamp": "2018-10-05T13:29:57.645+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@3cb8b584; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@3cb8b584; line: 1, column: 1]",
    "path": "/api/v1/customer/customers/search",
    "errors": []
}

What is the problem? Do you see any problem here or it may be caused because of my service class or dto classes ?

1
  • If you look at @RequestBody List<Long> ids then you can tell that it takes a list (JSON equivalent JSONArray). You can change it to Map if you want to stick to { "ids": [1,7,8] } Commented Oct 5, 2018 at 13:53

1 Answer 1

3

Try requesting with the payload [1,7,8], not {"ids": [1,7,8]}.

Your JSON would translate to a request body with the next format.

class Body {

    private List<Long> ids;

    // constructor, getters and setters 

}

For a REST client, you can take a look at RestTemplate.

RestTemplate template;
List<Long> ids;
List<CustomerDto> = template.exchange(
            "/customer/customers/search",
            HttpMethod.POST,
            new HttpEntity<>(ids),
            new ParameterizedTypeReference<List<CustomerDto>>() {}).getBody()
Sign up to request clarification or add additional context in comments.

4 Comments

now I have got 500 internal server error but probably it is because of my code right ? i mean now the json request format is correct probably.
What does the exception say on the server side?
Are you able to access the ids on the server with the debugger? If you can, your endpoint works as expected, at least on the request side.
2018-10-05 17:07:55,020 ERROR [] o.a.c.c.C.[.[.[.[dispatcherServlet]:181 - Servlet.service() for servlet [dispatcherServlet] in context with path [/api/v1] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null at com.customer.customers.service.customerService.findCustomerByIds(LocGrpService.java:33)

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.