jersey restful 测试_Jersey实现Restful服务(实例讲解)

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

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

e4a82a6bb04c033f50e09eb8ad473f14.png

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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

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;

/**

* Created by XuHui on 2017/8/2.

*/

@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;

/**

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

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

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

61658f748bdd58d8c1f049a521888646.png

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

POST请求

dc1e2c54da860c349b160f3031fb6fa1.png

GET请求

f3cc97e21f44cdd34a2edcd18d212212.png

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值