简体   繁体   English

具有相同休息终点的两个Java方法

[英]Two java methods with same rest end point

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. 我是一名从Java 5迁移到Java 1.7的Java开发人员,并且正在查看一些代码,但并不知道这是可能的。

@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. 我的问题是,如果调用的应用程序正在调用rootUrl / bookId / 1234,则它看起来像Java一样聪明,可以根据Response类型知道要调用的方法。

Am I right? 我对吗? Could someone please help with my understanding of this. 有人可以帮助我理解这一点。

thanks 谢谢

There is no Java language overriding or overloading going on here. 这里没有Java语言的重载或重载。 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. 您的问题与servlet容器路由请求有关,它基于许多因素来路由请求。 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. 这些因素之一可以是@Produces的值,该值与远程请求的accept标头中指定的媒体类型匹配。

Rest endpoint invocation will be determined by Accept header 其余端点的调用将由Accept标头确定

 curl -v -H "Accept: application/json" --data "param1=value1&param2=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&param2=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 ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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