2012-07-12 75 views
0

我想寫自動的序列化,並使用JAXB反序列化對象的一個​​簡單的Web服務:彈簧3和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那裏得到日誌,這使我很難確定根本原因。在這個過程中,我缺少一個關鍵的步驟嗎?

回答

0

通常,這是因爲請求中缺少「application/xml」的Accept頭或Content Type頭。此外,如果你使用Spring 3.1,可以讓你的註釋更明確的是這樣的:

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

是的,我需要修改我的'curl'要求以及:'捲曲-X POST -d「」 - H「Content-Type:application/xml」http:// localhost:9080/submit'。忘記了Spring有多特殊。 – 2012-07-12 22:46:05