简单的 RESTful Web 服务 Jersey编程用例 附代码
资源:
@Path("/contacts")
public class ContactsResource {
@Context
UriInfo uriInfo;
@Context
Request request;
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<Contact> getContacts() {
List<Contact> contacts = >new ArrayList<Contact>();
contacts.addAll( ContactStore.getStore().values() );
return contacts;
}
@Path("{contact}")
public ContactResource getContact(
@PathParam("contact") String contact) {
return new ContactResource(uriInfo, request, contact);
}
}
|
方法:
POST
@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void newContact(
@FormParam("id") String id,
@FormParam("name") String name,
@Context HttpServletResponse servletResponse
) throws IOException {
Contact c = new Contact(id,name,new ArrayList<Address>());
ContactStore.getStore().put(id, c);
URI uri = uriInfo.getAbsolutePathBuilder().path(id).build();
Response.created(uri).build();
servletResponse.sendRedirect("../pages/new_contact.html");
}
|
PUT
@PUT
@Consumes(MediaType.APPLICATION_XML)
public Response putContact(JAXBElement<Contact> jaxbContact) {
Contact c = jaxbContact.getValue();
return putAndGetResponse(c);
}
private Response putAndGetResponse(Contact c) {
Response res;
if(ContactStore.getStore().containsKey(c.getId())) {
res = Response.noContent().build();
} else {
res = Response.created(uriInfo.getAbsolutePath()).build();
}
ContactStore.getStore().put(c.getId(), c);
return res;
}
DELETE
@DELETE
public void deleteContact() {
Contact c = ContactStore.getStore().remove(contact);
if(c==null)
throw new NotFoundException("No such Contact.");
}
分享到:
相关推荐
NULL 博文链接:https://liangyingying1117.iteye.com/blog/2155855
Jersey is the open source (under dual CDDL+GPL license), production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services. But, it is also more than the Reference ...
使用SpringBoot整合jersey 实现Restful web service.同时整合springmvc。
Java使用Jersey框架构建 RESTful Web服务返回json数据所需的jar包。Jackson-core-asl.jar / Jackson-jaxrs.jar / Jackson-mapper-asl.jar / Jackson-xc.jar / jersey-media-json-Jackson.jar
Build a RESTful Web service using Jersey and Apache Tomcat document.
NULL 博文链接:https://tanling514.iteye.com/blog/2190901
泽西岛 RESTFUL Web服务的Jersey实现
RESTful Java Web Services (2009).pdf This book is for developers who want to code RESTful web services using the Java technology stack together with any of the frameworks Jersey's JAX-RS, Restlet's ...
RESTful webservices – Client API in Java - Jersey
在本文中,我希望向读者逐步说明如何使用Jersey 2.x创建RESTFul Web服务。
一个示例应用程序演示了Dropwizard如何无缝集成和实例化-Jetty服务器,RESTful Web服务的jersey框架,JSON的Jackson以及指标库。 介绍Dropwizard兼有-框架和库。 它由以下模块组成。 Jetty HTTP库和嵌入式HTTP...
但是在这篇文章中,我将展示如何使用具有基本身份验证的Jersey rest客户端使用RESTful Web服务。 ##我们需要的? RESTful URL。 泽西岛客户罐。 杰克逊(Jackson)数据映射器jar。 ## 1。 RESTful网址 使用...
REST 软件架构基于具象资源传输。RESTful Web 服务提供了一些优势:简单、轻量级、快速。RESTful Web 服务公开了一组由 URI 标识的资源。...Jersey 是 JAX-RS 的参考实现,简化了 Java 中 RESTful Web 服务的开发。
NULL 博文链接:https://liangyingying1117.iteye.com/blog/2061846
Messenger-应用程序- 使用 Jersey Restful Web 服务的 Messenger 应用程序。 这是一个具有 Restful Web 服务的简单 Messenger 应用程序。 应用程序具有服务层和具有静态数据的数据库类。 没有进行后端连接,因为它...
This book provides a comprehensive introduction to Jersey framework (an implementation of JAX-RS specification) as the application development framework for RESTFul web service development. This book ...