Dario Pascual
Dario Pascual

Reputation: 45

Java REST jersey produces URL

I have a method that return java.net.URL and I can´t find the right MediaType for the @Produces annotation. Is possible to produce an URL MediaType?

My code:

@GET
@Produces(MediaType.TEXT_HTML) //MediaType I don´t know
@Path("terms.html")
public URL getTerms(){

    Iterator<String> it=definitions.keySet().iterator();
    URL url,url1=null;
    try {
        url = new URL("http://localhost:8080/Dictionary/definitions/");
        url1 = new URL(url,it.next()+".html");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return url1;
}    

What I want in my rest service is something like that:

enter image description here

Upvotes: 2

Views: 376

Answers (2)

cassiomolin
cassiomolin

Reputation: 131137

As far as I know, there's no content type for URLs.

You can assume the URL you are returning is a piece of plain text and use MediaType.TEXT_PLAIN:

@GET
@Path("terms.html")
@Produces(MediaType.TEXT_PLAIN)
public URL getTerms() {
    ...
}

If you need, you can return a HTML document (using MediaType.TEXT_HTML) containing an <a> element, referencing the URL you want to return.

Upvotes: 1

dcsohl
dcsohl

Reputation: 7406

The media type is meant to denote what sort of document is being returned. And while there are "HTML documents" and "XML documents" and "Microsoft word documents" - each of which has a MIME type you could use - there isn't any such thing as a "URL document". So, it makes sense not to have a corresponding media type. If you are literally just returning the URL with no HTML/JSON/XML/whatever, then I'd use text.

Upvotes: 1

Related Questions