简体   繁体   中英

Content type when accept header is empty or unknown jax-rs

What is the default content returned when accept header is empty?

The below code returns application/xml when accept header is empty which maps to findAll(). Is there a way to force jax-rs to execute findAllAtom() when accept header is empty or unknown. I am using restEasy version 2 with Jboss Application server and 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;
    }
}

Request without Accept header means that client expects anything, like if it has specified */* . Basically if you have two methods that differ only by @Produces and Accept header means "any", there is no way for a JAX-RS framework how to choose the method, so according to spec it chooses the first one (See JSR-311 3.7.2)

I believe that the best solution will be sending Accept header with an exact type. Otherwise you can differ methods by different URLs: add @Path("/xml") and @Path("/atom") to the methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM