I am new to RESTful services. I usually develop Java EE applications and SOAP services in a JBoss / Wildfly environment. I am currently trying to find my way into RESTful services to broaden my knowledge. Since I am feeling familiar with JBoss / Wildfly I decided to go with RESTEasy.
I decided to create a RESTful service for an example pet shop chain. As a chain the pet shop has multiple stores which are identfied by a shop id (eg shop1, shop2, etc.). I have created multiple REST services to segment services based on technical functionality (eg article services => article.war, order service => orde.war, etc.
I want to create human readable URLs, like:
GET:
http://mypetshop.example/rest/ {shopId}/article/{articleId}
POST with JSON formatted order content:
http://mypetshop.example/rest/ {shopId}/order/create
So far I have only managed to create URLs like:
GET:
http://mypetshop.example/rest/article/ {shopId}/{articleId}
POST with JSON formatted order content:
http://mypetshop.example/rest/order/create/ {shopId}
Is my wanted REST path possible or do I have to keep up with my current solution?
Best regards, CB
Here is an example code for article services:
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;
}
}
Yes you can do
like below
rest/orders/1/completed
Here rest in rest servlet path , orders for class, then using @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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.