Spring 3.x JSON status 406 “characteristics not acceptable according to the request ”accept“ headers ()”

后端 未结 14 1465
太阳男子
太阳男子 2020-12-13 01:50

Upon trying to get my response in JSON using Spring 3.x, I get the 406 error \"The resource identified by this request is only capable

14条回答
  •  一个人的身影
    2020-12-13 02:38

    Don't make the same mistake I did, spend all day playing around with Spring configuration, when actually your object returned in a web service is not marshaling to XML correctly. It seems Spring catches a JAXB marshaling error and doesn't report it. Use this sandbox code to validate JAXB marshaling:

    MyClass myclass = new MyClass();
    //populate myclass here
    
    JAXBContext context = JAXBContext.newInstance(MyClass.class);
    
    Marshaller m = context.createMarshaller();
    StringWriter w = new StringWriter();
    
    m.marshal(myclass, w);
    System.out.println(w);
    

    This produced and displayed an exception. Fixed the cause, and my web service is available in both XML and JSON.

提交回复
热议问题