简体   繁体   中英

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

I'm getting issue when i'm hitting 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.

@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. If your data is xml you can do it eg with a JAXB2-Marshaller. If you have JSON-data you can use FasterXML(Jackson) in the same way. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM