栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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


 4.0.0
 com.restful
 jerseyDemo
 war
 1.0-SNAPSHOT
 jerseyDemo Maven Webapp
 http://maven.apache.org
 
 
  junit
  junit
  3.8.1
  test
 
 
  org.glassfish.jersey.containers
  jersey-container-servlet
  2.9
 
 
  org.glassfish.jersey.core
  jersey-client
  2.9
 
 
  org.glassfish.jersey.media
  jersey-media-json-jackson
  2.9
 
 
  com.sun.jersey
  jersey-client
  1.19.3
 
 
 
 jerseyDemo
 

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;


@Path("/JerseyService")
public class JerseyService {
 private static Map map = new HashMap();

 @GET
 @Path("/getAllResource")
 @Produces(MediaType.APPLICATION_JSON)
 public String getAllResource() throws JsonProcessingException {
  List list = new ArrayList();
  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;


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配置。




 Archetype Created Web Application
 
 Jersey RESTful Application
 org.glassfish.jersey.servlet.ServletContainer
 
  jersey.config.server.provider.packages
  com.restful
 
 
 
 Jersey RESTful Application
 /rest/*
 

5.搭建本地tomcat

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

POST请求

GET请求

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144955.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号