I'm a java developer moving from Java 5 to Java 1.7 and I'm looking at some code not knowing that it was possible.
@Path("/myroot")
@Service
public class MyServiceClass {
@POST
@Produces({ "application/x-protobuf" })
@Path("bookid/{bookNumber}")
public Response findBookByBookId(
@PathParam("bookNumber") String bookNumber,
@QueryParam("searchType") String searchType) {
return ...
}
@POST
@Produces("application/json")
@Path("bookid/{bookNumber}")
public Response findBookByBookIdAsJson(
@PathParam("bookNumber") String bookNumber,
@QueryParam("searchType") String searchType) {
return ...;
}
I've got two methods here with the same rest end point. The only difference is that they produce different resonse types.
My question is if the calling application is calling rootUrl/bookId/1234 it looks like java is smart enough to know what method to call based on the Response type.
Am I right? Could someone please help with my understanding of this.
thanks
There is no Java language overriding or overloading going on here. The methods have different names.
Your question has to do with the servlet container routing requests, and it routes requests based on a number of factors. One of those factors can be the value of @Produces
, which is matched to the media types specified in the remote request's accept
header.
Rest endpoint invocation will be determined by Accept
header
curl -v -H "Accept: application/json" --data "param1=value1¶m2=value2" http://<Server>/bookid/{bookNumber}
will invoke
@POST
@Produces("application/json")
@Path("bookid/{bookNumber}")
public Response findBookByBookIdAsJson(
@PathParam("bookNumber") String bookNumber,
@QueryParam("searchType") String searchType) {
return ...;
}
And
curl -v -H "Accept: application/x-protobuf" --data "param1=value1¶m2=value2" http://<Server>/bookid/{bookNumber}
will invoke
@POST
@Produces({ "application/x-protobuf" })
@Path("bookid/{bookNumber}")
public Response findBookByBookId(
@PathParam("bookNumber") String bookNumber,
@QueryParam("searchType") String searchType) {
return ...
}
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.