我试图巩固我对休息的理解。
1)当没有@Path时,如何调用此函数,具体来说,是什么触发getCustomers被调用? " public StreamingOutput getCustomers(final @QueryParam(" start")int start, final @QueryParam(" size")@DefaultValue(" 2")int size)"
来源:
package com.restfully.shop.services;
import com.restfully.shop.domain.Customer;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@Path("/customers")
public class CustomerResource
{
private Map<Integer, Customer> customerDB = Collections.synchronizedMap(new LinkedHashMap<Integer, Customer>());
public CustomerResource()
{
Customer customer;
int id = 1;
customer = new Customer();
customer.setId(id);
customer.setFirstName("Bill");
customer.setLastName("Burke");
customerDB.put(id++, customer);
customer = new Customer();
customer.setId(id);
customer.setFirstName("Joe");
customer.setLastName("Burke");
customerDB.put(id++, customer);
}
@GET
@Produces("application/xml")
public StreamingOutput getCustomers(final @QueryParam("start") int start,
final @QueryParam("size") @DefaultValue("2") int size)
{
return new StreamingOutput()
{
public void write(OutputStream outputStream) throws IOException, WebApplicationException
{
PrintStream writer = new PrintStream(outputStream);
writer.println("<customers>");
synchronized (customerDB)
{
int i = 0;
for (Customer customer : customerDB.values())
{
if (i >= start && i < start + size) outputCustomer(" ", writer, customer);
i++;
}
}
writer.println("</customers>");
}
};
}
@GET
@Produces("application/xml")
@Path("uriinfo")
public StreamingOutput getCustomers(@Context UriInfo info)
{
int start = 0;
int size = 2;
if (info.getQueryParameters().containsKey("start"))
{
start = Integer.valueOf(info.getQueryParameters().getFirst("start"));
}
if (info.getQueryParameters().containsKey("size"))
{
size = Integer.valueOf(info.getQueryParameters().getFirst("size"));
}
return getCustomers(start, size);
}
protected void outputCustomer(String indent, PrintStream writer, Customer cust) throws IOException
{
writer.println(indent + "<customer id=\"" + cust.getId() + "\">");
writer.println(indent + " <first-name>" + cust.getFirstName() + "</first-name>");
writer.println(indent + " <last-name>" + cust.getLastName() + "</last-name>");
writer.println(indent + "</customer>");
}
}
2)对于第二个getCustomers函数,我是否更正了 路径* @Path的存在(&#34; uriinfo&#34; )匹配触发呼叫的URL?注意#1根本没有@path。我知道那里有优先规则,但如果我有一个几乎相同的方法具有相同的路径会发生什么,那么BOTH方法会被调用吗?
让我说我也有这个 - 请注意getCustomers2中的2:
@GET
@Produces("application/xml")
@Path("uriinfo")
public StreamingOutput getCustomers2(@Context UriInfo info)
{
int start = 0;
int size = 2;
if (info.getQueryParameters().containsKey("start"))
{
start = Integer.valueOf(info.getQueryParameters().getFirst("start"));
}
if (info.getQueryParameters().containsKey("size"))
{
size = Integer.valueOf(info.getQueryParameters().getFirst("size"));
}
return getCustomers(start, size);
}
我收到了编译错误。
抱歉,我之前犯了一个错误,如果我在课堂上有这两种方法怎么办?
@GET
@Produces("application/xml")
public StreamingOutput getCustomers3(final @QueryParam("start") int start,
final @QueryParam("size") @DefaultValue("2") int size)
{
return new StreamingOutput()
{
public void write(OutputStream outputStream) throws IOException, WebApplicationException
{
PrintStream writer = new PrintStream(outputStream);
writer.println("<customers>");
synchronized (customerDB)
{
int i = 0;
for (Customer customer : customerDB.values())
{
if (i >= start && i < start + size) outputCustomer(" ", writer, customer);
i++;
}
}
writer.println("</customers>");
}
};
}
@GET
@Produces("application/xml")
public StreamingOutput getCustomers4(final @QueryParam("start") int start,
final @QueryParam("size") @DefaultValue("2") int size)
{
return new StreamingOutput()
{
public void write(OutputStream outputStream) throws IOException, WebApplicationException
{
PrintStream writer = new PrintStream(outputStream);
writer.println("<customers>");
synchronized (customerDB)
{
int i = 0;
for (Customer customer : customerDB.values())
{
if (i >= start && i < start + size) outputCustomer(" ", writer, customer) ;
i++;
}
}
writer.println("</customers>");
}
};
}
答案 0 :(得分:1)
当为getCustomers
发送HTTP GET
请求并且客户端请求/customers
作为请求的所需内容类型时,将调用application/xml
方法。< / p>
如果JAX-RS方法未使用@Path
注释进行注释,则它将继承类级别注释指定的路径。
当为getCustomers
发送HTTP GET
请求时,将调用第二个/customers/uriinfo
方法,因为路径是附加的,并且客户端请求application/xml
作为请求的所需内容类型。
不,@Path
值必须是唯一的。必须唯一的是HTTP方法(@GET
,@POST
,@DELETE
等),@Path
值以及@Produces
定义的内容类型的组合和@Consumes
方法。