3

(I'm new to java world)

I'm learning dropwizard and I want to create resource that is returning view (html) or json depending on request type (ajax or not)

Example:

@Path("/")
public class ServerResource {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public MainView getMainView() {
        return new MainView("Test hello world");
    }
}

How to add to this resource at the same Path JSON response if request is AJAX?

UPDATE 1. I created something like this:

@Path("/")
public class ServerResource {

    @GET
    @Consumes(MediaType.TEXT_HTML)
    @Produces(MediaType.TEXT_HTML)
    public MainView getMainView(@HeaderParam("X-Requested-With") String requestType) {
        return new MainView("hello world test!");
    }

    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> getJsonMainView() {
        List<String> list = new ArrayList<String>();
        for (Integer i = 0; i < 10; i++) {
            list.add(i, "test" + i.toString());
        }
        return list;
    }
}

Looks like this is working as expected, but I know that is not a good practice.

sliwinski.lukas
  • 1,412
  • 1
  • 18
  • 28
  • If you want to send JSON as well as html you must include it in your annotation like this: @Produces(MediaType.TEXT_HTML, MediaType.APPLICATION_JSON) – Paulino III Oct 10 '13 at 14:43

2 Answers2

6

Ajax requests USUALLY have (not always) X-Requested-With: XMLHttpRequest request header. See How to differentiate Ajax requests from normal Http requests?

The following code hasn't been tested.

@Path("/")
public class ServerResource {
    @GET
    @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON})
    public MainView getMainView(@HeaderParam("X-Requested-With") String requestType) {
        if(requestType != null && requestType.equals("XMLHttpRequest")) {
           //The request is AJAX
        } else {
           //The request is not AJAX
        }
        ...
    }
}
Community
  • 1
  • 1
Paulino III
  • 1,826
  • 1
  • 15
  • 15
  • @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) but how return for example List of String when request is XMLHttpRequest? I can't do that becouse return type is MainView – sliwinski.lukas Oct 10 '13 at 16:14
  • Well, that's another story; the question was how to detect an ajax request. Anyways, you can use Response as the return type and include the object within. It should be something like: return Response.status(200).entity(new MainView("hello world test!")).build(); OR return Response.status(200).entity(list).build(); If you use Response as the return type you'll be able to use the same method for both. – Paulino III Oct 10 '13 at 18:08
0

There is no difference between AJAX-request and just request for the server. It's just GET, POST, PUT, DELETE or HEAD. If you want to separate output you should mark it somehow in the request itself by adding query parameter or use another URL or adding some header and then parsing in inside your processing method.

Hope that makes sense.

zjor
  • 994
  • 2
  • 12
  • 22
  • 1
    You can achieve this by assigning a specific request header for those ajax requests. Check out this post: http://stackoverflow.com/questions/13344452/jax-rs-and-xmlhttp-communication – Paulino III Oct 10 '13 at 14:59
  • The header parameter "X-Requested-With" can be used – Edd Jul 09 '14 at 10:32