1. Create a rest folder with JAXRSConfiguration.java:

package com.pluralsight.bookstore.rest;

        import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application; @ApplicationPath("api")
public class JAXRSConfiguration extends Application { }

2. Create Endpoint file: BookEndpoint.java:

@Path("/books")
public class BookEndpoint { @Inject
private BookRepository bookRepository; }

package com.pluralsight.bookstore.rest;

import com.pluralsight.bookstore.model.Book;
import com.pluralsight.bookstore.repository.BookRepository; import javax.inject.Inject;
import javax.validation.constraints.Min;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo; import java.net.URI;
import java.util.List; import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
//api/books
@Path("/books")
public class BookEndpoint { @Inject
private BookRepository bookRepository; @POST
@Consumes(APPLICATION_JSON)
public Response createBook(Book book, @Context UriInfo uriInfo) {
book = bookRepository.create(book);
URI createedURI = uriInfo.getBaseUriBuilder().path(book.getId().toString()).build();
return Response.created(createedURI).build();
} @GET
@Produces(APPLICATION_JSON)
public Response getBooks() {
List<Book> books = bookRepository.findAll(); if(books.size() == 0) {
return Response.noContent().build();
}
return Response.ok(books).build();
} @GET
@Path("/{id: \\d+}")
@Produces(APPLICATION_JSON)
public Response getBook(@PathParam("id") @Min(1) Long id) {
Book book = bookRepository.find(id); if(book == null) {
return Response.status(Response.Status.NOT_FOUND).build();
} return Response.ok(book).build();
} // api/books/count
@GET
@Path("/count")
@Produces(PLAIN_TEXT)
public Response countBooks() {
Long nbOfBooks = bookRepository.countAll(); if(nbOfBooks == 0) {
return Response.noContent().build();
} return Response.ok(nbOfBooks).build();
} @DELETE
@Path("/{id : \\d+}")
public Response deleteBook(Long id) {
bookRepository.delete(id);
return Response.noContent().build();
}
}

[JavaEE] Implement a REST Endpoint的更多相关文章

  1. [JavaEE] Implement a test for REST endpoint

    1. We have the BookEndpoint.java: package com.pluralsight.bookstore.rest; import com.pluralsight.boo ...

  2. Service Station - An Introduction To RESTful Services With WCF

    Learning about REST An Abstract Example Why Should You Care about REST? WCF and REST WebGetAttribute ...

  3. Service Discovery in WCF 4.0 – Part 2 z

    Service Discovery in WCF 4.0 – Part 2 In the previous post I discussed about the basic usage of WCF ...

  4. Service Discovery in WCF 4.0 – Part 1 z

    Service Discovery in WCF 4.0 – Part 1 When designing a service oriented architecture (SOA) system, t ...

  5. JavaEE路径陷阱之getRealPath

    转自:http://blog.csdn.net/shendl/article/details/1427637   JavaEE路径陷阱之getRealPath   本文是<Java路径问题最终解 ...

  6. 【JavaEE WEB 开发】Tomcat 详解 Servlet 入门

    转载请注明出处 :  http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...

  7. Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)

    http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-an ...

  8. [JavaEE] Injecting Bean

    So what is a Bean, in JavaEE, any class expect Entity are Bean. One usefully thing in Bean is Depend ...

  9. 【JavaEE WEB 开发】Tomcat 具体解释 Servlet 入门

    转载请注明出处 :  http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...

随机推荐

  1. how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )

    Today, I had spent 3 hours to fix one problem, The old program has a bug, originally, when a user pr ...

  2. 关于defineProperty

    var c=0; Object.defineProperty(this,'b',{ get :function(){ return ++c; } }) console.log(b===1&&a ...

  3. Microsoft SQL Server学习(五)--操作符聚合函数

    算术运算符 逻辑运算符 比较运算符 聚合函数 算术运算符(+ - * / ) select score*2 as 成绩翻倍 from class_A update class_A set score= ...

  4. Farseer.net轻量级开源框架 入门篇:使用前说明

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 框架性能测试 下一篇:Farseer.net轻量级开源框架 入门篇: 增.删.改. ...

  5. npm换淘宝源 yarn换淘宝源

    查询初始的源 npm get registry > https://registry.npmjs.org/ 设置淘宝源 npm config set registry http://regist ...

  6. Vue的特性

    1.数据驱动视图 <div id="app"> <p> {{ message }}<p> </div> var app = new ...

  7. IMDB电影排行爬取分析

    一.打开IMDB电影T250排行可以看见250条电影数据,电影名,评分等数据都可以看见 按F12进入开发者模式,找到这些数据对应的HTML网页结构,如下所示 可以看见里面有链接,点击链接可以进入电影详 ...

  8. Uploadify上传大文件

    一丶参考地址 <script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthe ...

  9. HDU多校Round 7

    Solved:2 rank:293 J. Sequense 不知道自己写的什么东西 以后整数分块直接用 n / (n / i)表示一个块内相同n / i的最大i #include <bits/s ...

  10. BZOJ5314: [Jsoi2018]潜入行动 (树形DP)

    题意:一棵树选择恰好k个结点放置监听器 每个监听器只能监听相邻的节点 问能使得所有节点被监听的种类数 题解:反正就是很well-known的树形DP了 至于时间复杂度为什么是nk 不会不学 很好想到四 ...