[JavaEE] Implement a REST Endpoint
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的更多相关文章
- [JavaEE] Implement a test for REST endpoint
1. We have the BookEndpoint.java: package com.pluralsight.bookstore.rest; import com.pluralsight.boo ...
- 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 ...
- 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 ...
- 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 ...
- JavaEE路径陷阱之getRealPath
转自:http://blog.csdn.net/shendl/article/details/1427637 JavaEE路径陷阱之getRealPath 本文是<Java路径问题最终解 ...
- 【JavaEE WEB 开发】Tomcat 详解 Servlet 入门
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...
- 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 ...
- [JavaEE] Injecting Bean
So what is a Bean, in JavaEE, any class expect Entity are Bean. One usefully thing in Bean is Depend ...
- 【JavaEE WEB 开发】Tomcat 具体解释 Servlet 入门
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/47146817 一. Tomcat 下载安装配置 1. Tomcat 下载 T ...
随机推荐
- LN : leetcode 242 Valid Anagram
lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...
- css边框样式、边框配色、边框阴影、边框圆角、图片边框
边框样式 点线式边框 破折线式边框 直线式边框 双线式边框 槽线式边框 脊线式边框 内嵌效果的边框 突起效果的边框 <div style="width: 300px; height: ...
- js中获取class封装
1.封装 //封装getClass function getClass(tagName,className) //获得标签名为tagName,类名className的元素 { if(document. ...
- Angular——内置服务
$location <!DOCTYPE html> <html lang="en" ng-app="App"> <head> ...
- [Windows Server 2012] 初识Windows Server 2012
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:初次见识Win ...
- 如何实现ADSL宽带用户开机自动拨号与定时拨号
在宽带拨号网络的环境下,要通过手动拨号认证才能上网.下面给大家介绍怎么设置开机自动拨号上网以及定时拨号上网. 这也是为一个叫CHY的2B准备的技术套餐,不需要用到网上说的自动拨号软件,只要在主机上设置 ...
- 15Microsoft SQL Server 数据库维护
Microsoft SQL Server 数据库维护 2.6.1数据库联机与脱机 --联机:该状态为数据库正常状态,也就是我们常看到的数据库的状态,该状态下的数据库处于可操作状态,可以对数据库进行任何 ...
- 去重 取最大的一条sql
select T.BILL_CODE,t.SCAN_TYPE,t.PIECE,SCAN_SITE,SCAN_MAN, row_number() over(partition by t.bill_cod ...
- 微信小程序 导航 4种页面跳转 详解
1.wx.navigateTo 保留当前页面,跳转到应用内的某个页面,目前页面路径最多只能十层. 参数:url(可携带参数) .success .fail .complete 可用wxml代替: ...
- Python学习第二阶段,day1, 装饰器,生成器,迭代器
装饰器 不得不说,这是对初学者最难以理解的概念了,虽然我学过面向对象,但还是被搞懵逼了..前面还好理解,主要是后面“装饰器的装饰器”我理解不了.装饰器工厂,根据传入的参数不同去返回不同的装饰器,我不得 ...