簡體   English   中英

Json無法在Spring中反序列化實例錯誤

[英]Json can not deserialize instance Error in Spring

我正在嘗試發送json長列表並從db獲取記錄。

我的控制器是:

@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);
    }

}

客戶類是:

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

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

我用JSON在郵遞員中測試此服務:

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

但是我得到這個錯誤:

{
    "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": []
}

問題是什么? 您是否在這里看到任何問題,或者可能是由於我的服務類或dto類引起的?

嘗試使用有效負載[1,7,8]而不是{"ids": [1,7,8]}

您的JSON將轉換為具有以下格式的請求正文。

class Body {

    private List<Long> ids;

    // constructor, getters and setters 

}

對於REST客戶端,您可以看一下RestTemplate

RestTemplate template;
List<Long> ids;
List<CustomerDto> = template.exchange(
            "/customer/customers/search",
            HttpMethod.POST,
            new HttpEntity<>(ids),
            new ParameterizedTypeReference<List<CustomerDto>>() {}).getBody()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM