日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jersey创建restful服务及调用_Jersey实现Restful服务(实例讲解)

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jersey创建restful服务及调用_Jersey实现Restful服务(实例讲解) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jersey 是基于Java的一個輕量級RESTful風格的Web Services框架。以下我基于IDEA實現Restful完整Demo。

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

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

6.運行結果、http://localhost:8080/jerseyDemo/rest/application.wadl是所有對外接口的調用方法。使用postman來看看這個接口是怎么調用的吧。

POST請求

GET請求

以上這篇Jersey實現Restful服務(實例講解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的jersey创建restful服务及调用_Jersey实现Restful服务(实例讲解)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。