【问题标题】:Spring 3 and JAXB in Web ServicesWeb 服务中的 Spring 3 和 JAXB
【发布时间】:2012-07-12 21:30:47
【问题描述】:

我正在尝试编写一个使用 JAXB 自动序列化和反序列化对象的简单 Web 服务:

@XmlRootElement
public class SimpleObject {

    private int id;

    private String name;

    /* ... */
}

@Controller
public class SimpleObjectController {

    @RequestMapping("submit",method=RequestMethod.POST)
    public String doPost(@RequestBody SimpleObject value) {
        return value.toString();
    }
}

<?xml version="1.0"?>
<beans ...>

    <oxm:jaxb2-marshaller id="marshaller" class="path.to.objects"/>
</beans>

然而,当我实际提出请求时,我得到以下信息:

HTTP Status 415
The server refused this request because the request entity is in a format not
supported by the requested resource for the requested method ().

我没有从 Spring 收到任何日志,这让我很难确定根本原因。这个过程中是否有我遗漏的关键步骤?

【问题讨论】:

    标签: spring jaxb spring-ws


    【解决方案1】:

    这通常是因为您的请求中缺少“application/xml”的 Accept 标头或 Content Type 标头。此外,如果您使用的是 Spring 3.1,您可以通过这种方式使您的注释更加明确:

    @RequestMapping(value="submit",method=RequestMethod.POST, consumes="application/xml", produces="application/xml")
    

    【讨论】:

    • 是的,我还需要修改我的curl 请求:curl -X POST -d "&lt;content/&gt;" -H "Content-Type:application/xml" http://localhost:9080/submit。忘记了 Spring 的具体情况。
    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    相关资源
    最近更新 更多