1
接受標頭爲空時返回的默認內容是什麼?接受標頭爲空或未知的內容類型jax-rs
下面的代碼在accept header爲空時映射到findAll()時返回application/xml。有沒有辦法強制jax-rs在accept頭爲空或未知時執行findAllAtom()。 我使用RestEasy的版本2 JBoss應用服務器和Adbera 1.1.2
@Stateless
@Path("quotes")
public class QuoteFacadeREST extends AbstractFacade<Quote> {
@PersistenceContext(unitName = "RestFullDayTraderPU")
private EntityManager em;
public QuoteFacadeREST() {
super(Quote.class);
}
@GET
@Override
@Produces({"application/xml", "application/json"})
public List<Quote> findAll() {
return super.findAll();
}
@GET
@Override
@Produces({"application/atom+xml"})
@GET
public Feed findAllAtom() throws Exception {
Factory factory = abdera.getFactory();
Feed feed = abdera.getFactory().newFeed();
feed.setId("tag:example.org,2007:/foo");
feed.setTitle("Feed Title");
feed.setSubtitle("Feed subtitle");
feed.setUpdated(new Date());
feed.addAuthor("My Name");
feed.addLink("http://example.com");
feed.addLink("http://example.com","self");
Entry entry = feed.addEntry();
entry.setId("tag:example.org,2007:/foo/entries/2");
entry.setTitle("Entry title 22 44");
entry.setUpdated(new Date());
entry.setPublished(new Date());
entry.setSummary("Feed Summary");
entry.setContent("One line content");
return feed;
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
在我的測試用例中我使用rssOwl作爲其他客戶端。 rssOwl正在發送一個空的accept頭。這可能是其他ATOM客戶很少的情況。處理這種情況的一種方法是使用基於URL的內容協商,但我更喜歡使用標題,因此上述問題。 – 2011-04-13 11:55:22
你不能用純JAX-RS來做到這一點。使用Apache Wink支持內容類型快捷方式。快捷方式在url中作爲查詢參數傳遞,因此調用ATOM將爲http:// host:port/app?alt = atom。請參閱http://incubator.apache.org/wink/1.1/html/5.1%20Registration%20and%20Configuration.html – Tarlog 2011-04-13 13:24:50
我會接受您以前的建議。我要實現基於URL的內容協商,至少對於主要提要。 – 2011-04-13 14:19:55