私はRESTfulなサービスが初めてです。私は通常、JBoss/Wildfly環境でJava EEアプリケーションとSOAPサービスを開発しています。私は現在、私の知識を広げるために、RESTfulなサービスに私の道を見つけることを試みています。 JBoss/Wildflyに精通しているので、私はRESTEasyと一緒に行くことにしました。REST:最初のトークンとしてパラメータを使用してリクエストパスを構築するにはどうすればよいですか?
ペットショップチェーンの例として、RESTfulサービスを作成することにしました。チェーンとして、ペットショップは、店舗ID(例えば、店舗1、店舗2など)によって識別される複数の店舗を有する。
私は人間が読めるURLを作成したいと考えています:
GET :
http://mypetshop.example/rest/ {shopId} /記事/ {情報でarticleID} JSONフォーマットされた注文内容と
POST:これまでのところ私だけのようなURLを作成するために管理している
http://mypetshop.example/rest/ {shopId} /オーダー/
を作成します。
GET:JSONフォーマットされた注文内容と
http://mypetshop.example/rest/article/ {shopId}/{情報でarticleID}
POST:
{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;
}
}
お返事ありがとうございました。あなたの模範を私の必要に応じて採用しました。それは魅力のように機能します。ありがとうございました。 –