繁体   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