14

I have a restful web service method like this one:

@GET
@Path("/generateInfo")
@Produces(MediaType.APPLICATION_JSON)
public String generateInfo(
        @QueryParam("a") String a,
        @QueryParam("b") String b, 
        @QueryParam("date") Date date) {
    // ...business code...
    return "hello world";
}

How can I invoke that method from a WebBrowser?, the problem is the Date param that when i try is giving me 404 not found or 500 internal server error.

1
  • Will passing milliseconds work? Commented Jul 8, 2015 at 5:24

3 Answers 3

15

I would suggest accepting the date as a String and parsing it yourself. Like so:

@GET
@Path("/generateInfo")
@Produces(MediaType.APPLICATION_JSON)
public String generateInfo(
        @QueryParam("a") String a,
        @QueryParam("b") String b, 
        @QueryParam("date") String date) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date dateObj = sdf.parse(date);

    return "hello world";
}

The way to make this request via a browser is:

http://localhost/your_service/generateInfo?date=2013-02-14

Things to consider when parsing dates:

  • SimpleDateFormat is very flexible with parsing different date formats. The ISO standard for date strings is: yyyy-MM-dd

  • The Joda Java date API is accepted as a more complete implementation of date/time, and some believe that it is more optimised than Java's native date API particularly for parsing dates.

  • it is often better to provide dates as epoch timestamps, especially if your application operates over different timezones. However, you must be aware of HTTP caching issues when accepting epoch timestamps (e.g. if your clients are not truncating epoch timestamps then you will get lots of cache misses). I would refer back to ISO-8601 as formatted dates are easier to HTTP cache.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the preferred way IMHO. Your JSON marshaller most likely will break down the Date class into it's fields, so you'd end up with an object Date in your JSON, rather then a neat String.
0

It depends on the format of the date in the URL. According to this question, any format that the constructor Date(String) can handle should work. If your date format is different and you cannot change it, you'll have to go with pestrella's solution and receive the date as a string and convert it yourself.

Comments

0
@GET
@Path("/generateInfo")
@Produces(MediaType.APPLICATION_JSON)

public String generateInfo(
 @QueryParam("a") String a,   
 @QueryParam("b") String b, 
 @QueryParam("date") String str_date) {

 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

 Date date = df.parse(str_date);
 return "hello world";
}

1 Comment

This is an exact copy of the accepted answer with just minor changes in variable names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.