簡體   English   中英

無法讀取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException

[英]Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException

當我點擊URL時出現問題

我的控制器

請幫我找出標題中的錯誤

  @RestController
    @RequestMapping("/client")
    public class TestController {

        @PostMapping(produces = { "application/json", "application/xml" })

    public ResponseEntity<Client> createCustomer(@RequestBody Client customer) {

            System.out.println("Creat Customer: " + customer);

            return ResponseEntity.ok(customer);
        }

    }

如果您有一個“普通” spring web mvc,請嘗試此操作。

@RequestMapping(value="client", produces = { "application/json", "application/xml" })
public @ResponseBody Customer createCustomer(
    @RequestParam Customer customer) {

        ...do some work like customerDao.create(customer);

        System.out.println("Create Customer: " + customer);

        return customer;
}

如果這是REST接口,則需要首先反序列化傳入的數據。 如果您的數據是xml,則可以使用JAXB2-Marshaller進行。 如果您具有JSON數據,則可以以相同方式使用FasterXML(Jackson)。 您的代碼可能看起來像

@RequestMapping(value="client", produces = { "application/json", "application/xml" })
public @ResponseBody Customer createCustomer(
    @RequestBody String body) {

        Source source = new StreamSource(new StringReader(body));
        RestRequest restRequest = (RestRequest)jaxb2Marshaller.unmarshal(source);

        Customer customer = (Customer) restRequest.getRequestData();

        ...do some work like customerDao.create(customer);

        System.out.println("Create Customer: " + customer);

        return customer;
}

暫無
暫無

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

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