欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Jersey实现Restful服务(实例讲解)

程序员文章站 2023-12-22 19:34:34
jersey 是基于java的一个轻量级restful风格的web services框架。以下我基于idea实现restful完整demo。 1.创建maven-web工...

jersey 是基于java的一个轻量级restful风格的web services框架。以下我基于idea实现restful完整demo。

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

Jersey实现Restful服务(实例讲解)

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

Jersey实现Restful服务(实例讲解)

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

post请求

Jersey实现Restful服务(实例讲解)

get请求

Jersey实现Restful服务(实例讲解)

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

上一篇:

下一篇: