client.getLocation()缺少上下文根

时间:2016-01-19 11:05:27

标签: java eclipse rest jax-rs tomcat8

我正在使用jax-rs开发一个小型客户REST服务,并且使用response.getLocation()方法遇到了一些麻烦。

这是我正在使用的测试客户端的片段:

public static void main(String args[]) throws Exception {
    Client client = ClientBuilder.newClient();

    try {
        System.out.println("*** Create a new Customer ***");

        String xml = "<customer>"
                + "<firstName>John</firstName>"
                + "<lastName>Doe</lastName>"
                + "<street>Far</street>"
                + "<city>Away</city>"
                + "<state>FR</state>"
                + "<zip>99999</zip>"
                + "<country>USA</country>"
                + "</customer>";

        Response response = client.target(
                "http://localhost:8080/jaxrs/customers").request().post(Entity.xml(xml));

        if (response.getStatus() != 201) throw new RuntimeException("Failed to create");


        String location = response.getLocation().toString();
        System.out.println("Location: "+ location);
        response.close();

        System.out.println("*** GET Created Customer ***");
        String customer = client.target(location).request().get(String.class);
        System.out.println(customer);

我使用“jaxrs”作为Eclipse Mars定义的上下文根。客户正在被创建,但显然是因为我现在正在使用绝对路径。

但在执行response.getLocation()时,位置集为http://localhost:8080/customers/1。我需要http://localhost:8080/jaxrs/customers/1代替。

  • response.getLocation()中缺少上下文根的原因是什么?
  • 我是否必须在其他地方定义上下文根?
  • response.getLocation()方法不应该返回上下文根吗?

我正在使用Tomcat8和org.glassfish.jersey jar用于REST服务

感谢您的时间。

0 个答案:

没有答案