Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java REST:如何使用参数作为第一个令牌构建请求路径?_Java_Web Services_Rest_Wildfly_Resteasy - Fatal编程技术网

Java REST:如何使用参数作为第一个令牌构建请求路径?

Java REST:如何使用参数作为第一个令牌构建请求路径?,java,web-services,rest,wildfly,resteasy,Java,Web Services,Rest,Wildfly,Resteasy,我不熟悉RESTful服务。我通常在JBoss/Wildfly环境中开发JavaEE应用程序和SOAP服务。我目前正试图找到进入RESTful服务的途径,以拓宽我的知识面。因为我对JBoss/Wildfly很熟悉,所以我决定使用RESTEasy 我决定为一家宠物连锁店创建一个RESTful服务。作为一个连锁店,宠物店有多个店铺,这些店铺由店铺id标识(例如店铺1、店铺2等)。我创建了多个REST服务,以基于技术功能对服务进行细分(例如,article services=>article.war、

我不熟悉RESTful服务。我通常在JBoss/Wildfly环境中开发JavaEE应用程序和SOAP服务。我目前正试图找到进入RESTful服务的途径,以拓宽我的知识面。因为我对JBoss/Wildfly很熟悉,所以我决定使用RESTEasy

我决定为一家宠物连锁店创建一个RESTful服务。作为一个连锁店,宠物店有多个店铺,这些店铺由店铺id标识(例如店铺1、店铺2等)。我创建了多个REST服务,以基于技术功能对服务进行细分(例如,article services=>article.war、order service=>orde.war等)

我想创建人类可读的URL,如:
获取:
{shopId}/article/{articleId}

使用JSON格式的订单内容发布:
{shopId}/order/create

到目前为止,我只创建了如下URL:
获取:
{shopId}/{articleId}

使用JSON格式的订单内容发布:
{shopId}

我想要的REST路径是可能的还是必须跟上当前的解决方案

致以最良好的祝愿, CB

以下是文章服务的示例代码:

ArticleRestApplication.java:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath(ArticleRestApplication.ROOT_PATH)
public class OrderRestApplication extends Application {
    public static final String ROOT_PATH = "/article";
}
ArticleService.java

public interface ArticleService{
    Article getArticle(String shopId, Integer articleId);
}
ArticleServiceImpl.java:

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;

@Path("/")
@Consumes(MediaType.APPLICATION_JSON + ";charset=UTF-8")
@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
public class ArticleServiceImpl implements ArticleService {
    public ArticleServiceImpl() {
        super();
    }

    @GET
    @Path("/{shopId}/{articleId}")
    public Article getArtikel(
      @PathParam("shopId") String shopId,
      @PathParam("articleId") Integer articleId) {
        System.out.println(String.format("Shop ID: \"%s\"", shopId));
        System.out.println(String.format("Article ID: \"%s\"", articleId));
        return gson.toJson(new Article(articleId));
    }
}
Article.java:

import java.io.Serializable;
import java.math.BigDecimal;

import javax.xml.bind.annotation.XmlRootElement;

@SuppressWarnings("serial")
@XmlRootElement(name = "article")
public class Article implements Serializable {
    private String shopId;
    private int articleId;
    private String name = "Super pet food";
    private BigDecimal price = new BigDecimal("1.00");
    private int unitsInStock = 1000;

    public Article(String shopId, int articleId) {
        super();
        this.shopId = shopId;
        this.articleId = articleId;
    }
}
是的,你可以做
如下

剩余/订单/1/完成

这里rest在rest servlet路径中,为类排序,然后使用
@path(“{orderId}/completed”)


现场演示

感谢您的回复。我根据自己的需要采用了您的示例,效果非常好。谢谢。
 @Path("orders")
public class OrderService {

    @GET
    @Path("{orderId}/completed")
    public String getOrders(@PathParam("orderId") String orderId) {
        return "orderId: " + orderId;
    }

    @GET
    @Path("summary")
    public String getOrdersSummary() {
        return "orders summary";
    }
}