简体   繁体   English

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

[英]REST: How do I build a request path with a parameter as first token?

I am new to RESTful services. 我是RESTful服务的新手。 I usually develop Java EE applications and SOAP services in a JBoss / Wildfly environment. 我通常在JBoss / Wildfly环境中开发Java EE应用程序和SOAP服务。 I am currently trying to find my way into RESTful services to broaden my knowledge. 我目前正在尝试寻找进入RESTful服务的方式,以扩展我的知识。 Since I am feeling familiar with JBoss / Wildfly I decided to go with RESTEasy. 由于我对JBoss / Wildfly感到熟悉,因此决定选择RESTEasy。

I decided to create a RESTful service for an example pet shop chain. 我决定为示例宠物店链创建RESTful服务。 As a chain the pet shop has multiple stores which are identfied by a shop id (eg shop1, shop2, etc.). 作为一个连锁店,宠物店有多个商店,这些商店由商店ID标识(例如shop1,shop2等)。 I have created multiple REST services to segment services based on technical functionality (eg article services => article.war, order service => orde.war, etc. 我创建了多个REST服务,以基于技术功能对服务进行细分(例如,文章服务=> article.war,订购服务=> orde.war等。

I want to create human readable URLs, like: 我想创建可读的URL,例如:
GET: 得到:
http://mypetshop.example/rest/ {shopId}/article/{articleId} http://mypetshop.example/rest/ {shopId} / article / {articleId}

POST with JSON formatted order content: 使用JSON格式的订单内容进行POST:
http://mypetshop.example/rest/ {shopId}/order/create http://mypetshop.example/rest/ {shopId} / order / create

So far I have only managed to create URLs like: 到目前为止,我只设法创建了如下URL:
GET: 得到:
http://mypetshop.example/rest/article/ {shopId}/{articleId} http://mypetshop.example/rest/article/ {shopId} / {articleId}

POST with JSON formatted order content: 使用JSON格式的订单内容进行POST:
http://mypetshop.example/rest/order/create/ {shopId} http://mypetshop.example/rest/order/create/ {shopId}

Is my wanted REST path possible or do I have to keep up with my current solution? 我想要的REST路径是否可行,还是必须跟上当前的解决方案?

Best regards, CB 最好的问候,CB

Here is an example code for article services: 这是商品服务的示例代码:

ArticleRestApplication.java: 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 ArticleService.java

public interface ArticleService{
    Article getArticle(String shopId, Integer articleId);
}

ArticleServiceImpl.java: 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: 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;
    }
}

Yes you can do 是的,你可以做
like below 像下面

rest/orders/1/completed 休息/订单/ 1 /完成

Here rest in rest servlet path , orders for class, then using @Path("{orderId}/completed") 在这里休息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";
    }
}

Live demo at http://jerseyexample-ravikant.rhcloud.com/rest/orders/1/completed 现场演示, 网址http://jerseyexample-ravikant.rhcloud.com/rest/orders/1/completed

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

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