矩阵参数是URI路径中的一组“ name = value ”,例如,
/books/2011;author=mkyong
在上述URI中,矩阵参数为“ author = mkyong ”,以分号“”分隔; ”。
1. @MatrixParam示例
请参阅在JAX-RS中使用@MatrixParam
的完整示例。
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/books")
public class BookService {
@GET
@Path("{year}")
public Response getBooks(@PathParam("year") String year,
@MatrixParam("author") String author,
@MatrixParam("country") String country) {
return Response
.status(200)
.entity("getBooks is called, year : " + year
+ ", author : " + author + ", country : " + country)
.build();
}
}
请参阅以下URI模式和结果。
1. URI模式:“ / books / 2011 / ”
getBooks is called, year : 2011, author : null, country : null
2. URI模式:“ / books / 2011; author = mkyong ”
getBooks is called, year : 2011, author : mkyong, country : null
3. URI模式:“ / books / 2011; author = mkyong; country = malaysia ”
getBooks is called, year : 2011, author : mkyong, country : malaysia
4. URI模式:“ / books / 2011; country = malaysia; author = mkyong ”
getBooks is called, year : 2011, author : mkyong, country : malaysia
下载源代码
下载它– JAX-RS-MatrixParam-Example.zip (6 KB)
参考文献
翻译自: https://mkyong.com/webservices/jax-rs/jax-rs-matrixparam-example/