2011-04-13 70 views
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; 
    } 
} 

回答

3

請求,而不接受頭信息意味着客戶端希望任何東西,就像如果它已指定*/*。基本上,如果你有兩種方法只有@Produces不同,Accept頭意味着「any」,那麼JAX-RS框架如何選擇方法是沒有辦法的,所以根據規範選擇第一個方法(參見JSR-311 3.7 .2)

我相信最好的解決方案將發送一個確切類型的Accept頭。 否則,您可以通過不同的URL區分方法:將@Path("/xml")@Path("/atom")添加到方法中。

+0

在我的測試用例中我使用rssOwl作爲其他客戶端。 rssOwl正在發送一個空的accept頭。這可能是其他ATOM客戶很少的情況。處理這種情況的一種方法是使用基於URL的內容協商,但我更喜歡使用標題,因此上述問題。 – 2011-04-13 11:55:22

+1

你不能用純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

+0

我會接受您以前的建議。我要實現基於URL的內容協商,至少對於主要提要。 – 2011-04-13 14:19:55

相關問題