简体   繁体   English

无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException

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

I'm getting issue when i'm hitting URL 当我点击URL时出现问题

My Controller 我的控制器

Please help me out find my error from my title 请帮我找出标题中的错误

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

    }

Try this if you have a "normal" spring web mvc. 如果您有一个“普通” 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;
}

If this is a REST-Interface then you need to first deserialize your incoming data. 如果这是REST接口,则需要首先反序列化传入的数据。 If your data is xml you can do it eg with a JAXB2-Marshaller. 如果您的数据是xml,则可以使用JAXB2-Marshaller进行。 If you have JSON-data you can use FasterXML(Jackson) in the same way. 如果您具有JSON数据,则可以以相同方式使用FasterXML(Jackson)。 Your code could look like 您的代码可能看起来像

@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.

相关问题 无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:提取调用中缺少必需的请求正文 - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing From a Fetch Call org.springframework.http.converter.HttpMessageNotReadableException - org.springframework.http.converter.HttpMessageNotReadableException 无法实例化Json对象org.springframework.http.converter.HttpMessageNotReadableException - Cannot Instantiate Json Object org.springframework.http.converter.HttpMessageNotReadableException Rest API测试org.springframework.http.converter.HttpMessageNotReadableException - Rest API Testing org.springframework.http.converter.HttpMessageNotReadableException org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文 - org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing 发送POST请求时org.springframework.http.converter.HttpMessageNotReadableException - org.springframework.http.converter.HttpMessageNotReadableException when sending a POST request 这个错误是什么&#39;org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法反序列化实例&#39;是什么意思? - What does this error 'org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance' mean? 处理 DefaultHandlerExceptionResolver :org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:意外字符 - Handle DefaultHandlerExceptionResolver : org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character org.springframework.http.converter.HttpMessageNotReadableException在调用rest post调用时 - org.springframework.http.converter.HttpMessageNotReadableException While calling rest post call Spring-boot RestAPI发布org.springframework.http.converter.HttpMessageNotReadableException - Spring-boot RestAPI post org.springframework.http.converter.HttpMessageNotReadableException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM