发布于 2017-12-06 01:27:23 | 154 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Jersey RESTful的Web服务框架

Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTful Web service。此外Jersey还提供一些额外的API和扩展机制,所以开发人员能够按照自己的需要对Jersey进行扩展。


下面小编就为大家带来一篇Jersey实现Restful服务(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

jersey 是基于Java的一个轻量级RESTful风格的Web Services框架。以下我基于IDEA实现Restful完整Demo。

1.创建maven-web工程,后面就是正常的maven工程创建流程。

2.添加Jersey框架的maven文件。


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.restful</groupId>
 <artifactId>jerseyDemo</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>jerseyDemo Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
 </dependency>
 <dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet</artifactId>
  <version>2.9</version>
 </dependency>
 <dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-client</artifactId>
  <version>2.9</version>
 </dependency>
 <dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <version>2.9</version>
 </dependency>
 <dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.19.3</version>
 </dependency>
 </dependencies>
 <build>
 <finalName>jerseyDemo</finalName>
 </build>
</project>

3.Restful接口定义。


package com.restful.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by XuHui on 2017/8/2.
 */
@Path("/JerseyService")
public class JerseyService {
 private static Map<String, PersonEntity> map = new HashMap<String, PersonEntity>();

 @GET
 @Path("/getAllResource")
 @Produces(MediaType.APPLICATION_JSON)
 public String getAllResource() throws JsonProcessingException {
  List<PersonEntity> list = new ArrayList<PersonEntity>();
  for (PersonEntity entity : map.values()) {
   list.add(entity);
  }

  ObjectMapper mapper = new ObjectMapper();
  return mapper.writeValueAsString(list);
 }

 @GET
 @Path("/getResourceById/{id}")
 @Produces(MediaType.APPLICATION_JSON)
 public String getResource(@PathParam("id") String id) throws JsonProcessingException {
  ObjectMapper mapper = new ObjectMapper();
  return mapper.writeValueAsString(map.get(id));
 }

 @POST
 @Path("/addResource/{person}")
 @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 @Produces(MediaType.APPLICATION_JSON)
 public String addResource(String person) throws IOException {
  ObjectMapper mapper = new ObjectMapper();
  PersonEntity entity = mapper.readValue(person, PersonEntity.class);
  map.put(entity.getId(), entity);
  return mapper.writeValueAsString(entity);
 }

 @GET
 @Path("/getRandomResource")
 @Produces(MediaType.APPLICATION_JSON)
 public String getRandomResource() throws JsonProcessingException {
  ObjectMapper mapper = new ObjectMapper();
  PersonEntity entity = new PersonEntity("NO1", "Joker", "http:///");
  return mapper.writeValueAsString(entity);
 }
}

PersonEntity实体类实现。


package com.restful.entity;

/**
 * Created by XuHui on 2017/8/2.
 */
public class PersonEntity {
 private String id;
 private String name;
 private String addr;

 public PersonEntity() {
 }

 public PersonEntity(String id, String name, String addr) {
  this.id = id;
  this.name = name;
  this.addr = addr;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddr() {
  return addr;
 }

 public void setAddr(String addr) {
  this.addr = addr;
 }

 @Override
 public String toString() {
  return "PersonEntity{" +
    "id='" + id + '\'' +
    ", name='" + name + '\'' +
    ", addr='" + addr + '\'' +
    '}';
 }
}

4.web.xml配置。


<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
 <display-name>Archetype Created Web Application</display-name>
 <servlet>
 <servlet-name>Jersey RESTful Application</servlet-name>
 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
 <init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.restful</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>Jersey RESTful Application</servlet-name>
 <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>

5.搭建本地tomcat

6.运行结果、http://localhost:8080/jerseyDemo/rest/application.wadl是所有对外接口的调用方法。使用postman来看看这个接口是怎么调用的吧。

POST请求

GET请求

以上这篇Jersey实现Restful服务(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务