1.在myeclipse中新建一个Dynamic Web Project

2.下载jar包,地址在这里

3.restful service代码

package com.qy;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; //这里@Path定义了类的层次路径。
//指定了资源类提供服务的URI路径。
@Path("userService")
public class HelloRestFul {
private String name= ""; @GET //get请求
@Path("/hello/{name}") //指定了资源类提供服务的URI路径。
@Produces(MediaType.TEXT_XML) //返回xml类型
public String sayHello(@PathParam("name") String name){
this.name = name;
System.out.println("Hello "+this.name);
return "<root>" + "<Say>" + "Hello "+name + "</Say>" + "</root>";
} @GET
@Path("/bye/{name}")
@Produces(MediaType.TEXT_XML)
public String bye(@PathParam("name") String name) {
System.out.println("Bye "+ name);
return "<root>" + "<Say>" + "bye "+name + "</Say>" + "</root>";
}
}

4.在web.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>RESTful</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <!--restful容器类-->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name> <!--自动加载的包的参数-->
<param-value>com.qy</param-value> <!--restful自动加载指定包下的controller-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern> <!--请求地址时需要在项目和servicepath之间加上这个rest-->
</servlet-mapping>
</web-app>

5.测试service接口是否成功

6.确定service接口没问题了,在java中写调用接口的方法,jersey调用接口方法如下

    //创建请求对象
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(BASE_URI); //具体项目的WebResource
//具体路径的WebResource
WebResource helloResource = resource.path("rest").path(PATH_Hello + name);
   //返回请求结果的xml
String responseString = helloResource.accept(MediaType.TEXT_XML).get(String.class);
   //返回请求结果的状态
String clientResponseString = helloResource.accept(MediaType.TEXT_XML).get(ClientResponse.class).toString();

7.调用接口

package com.qy;

import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig; /**
* 调用接口
*/
public class HelloRestFulClient {
public static final String BASE_URI = "http://localhost:8080/RESTful";
public static final String PATH_Hello = "/userService/hello/";
public static final String PATH_Bye = "/userService/bye/"; public static void main(String[] args) {
//参数
String name = "qy"; //创建请求对象
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(BASE_URI); //具体项目的WebResource
//路径
WebResource helloResource = resource.path("rest").path(PATH_Hello + name);
System.out.println("Client Response 返回状态");
getClientResponse(helloResource);
System.out.println("Response 返回 xml" );
getResponse(helloResource);
System.out.println("----------------------------------------------------");
WebResource byeResource = resource.path("rest").path(PATH_Bye + name);
System.out.println("Client Response 返回状态");
getClientResponse(byeResource);
System.out.println("Response 返回 xml" );
getResponse(byeResource);
} /**
* 调用WebResource,返回客户端请求状态。
* GET http://localhost:8080/RESTful/rest/userService/hello/qiuyu
* 返回请求结果状态“200 OK”。
*/
private static String getClientResponse(WebResource resource) {
String clientResponseString = resource.accept(MediaType.TEXT_XML)
.get(ClientResponse.class).toString();
System.out.println(clientResponseString);
return clientResponseString;
} /**
* 调用WebResource,返回请求结果XML
* <root><Say>bye qiuyu</Say></root>
*/
private static String getResponse(WebResource resource) {
String responseString = resource.accept(MediaType.TEXT_XML)
.get(String.class);
System.out.println(responseString);
return responseString;
}
}

8.执行返回结果

源码

用jersey写简单Restful接口的更多相关文章

  1. flask 搭建简单restful接口,moco基础

    from flask import Flask, jsonify, abort, make_response app = Flask(__name__)app.config['JSON_AS_ASCI ...

  2. Jersey 写restful接口时QueryParam ,FormParam 等的区别

    今天用jersey写接口,发现有一个post方法中没有得到参数,查了半天发现自己一不小心将@formparam写成了@queryparam,真是一个悲伤的故事.在这里把几个参数类型整理了一下放出来. ...

  3. 用flask写一个简单的接口

    用falsk写一个简单的接口,这个接口的数据本来是爬虫爬取的数据,但是今天只写一个flask接口,数据就用测试数据好了. import random import re import time imp ...

  4. [转]简单识别 RESTful 接口

         本文描述了识别一个接口是否真的是 RESTful 接口的基本方法.符合 REST 架构风格的接口,称为 RESTful 接口.本文不打算从架构风格的推导方面描述,而是从 HTTP 标准的方面 ...

  5. Swagger+Spring mvc生成Restful接口文档

    简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...

  6. 网络协议 21 - RPC 协议(中)- 基于 JSON 的 RESTful 接口协议

        上一节我们了解了基于 XML 的 SOAP 协议,SOAP 的 S 是啥意思来着?是 Simple,但是好像一点儿都不简单啊! 传输协议问题     对于 SOAP 来讲,比如我创建一个订单, ...

  7. python 全栈开发,Day100(restful 接口,DRF组件,DRF跨域(cors组件))

    昨日内容回顾 1. 为什么要做前后端分离? - 前后端交给不同的人来编写,职责划分明确.方便快速开发 - 针对pc,手机,ipad,微信,支付宝... 使用同一个接口 2. 简述http协议? - 基 ...

  8. vue项目构建实战基础知识:SPA理解/RESTful接口介绍/static目录配置/axios封装/打包时map文件去除

    一.SPA 不是指水疗.是 single page web application 的缩写.中文翻译为 单页应用程序 或 单页Web应用,更多解释请自行搜索. 所有的前端人员都应该明白我们的页面的 u ...

  9. RESTful 接口实现简明指南

    REST 简介 REST 是一个术语的缩写,REpresentational State Transfer,中文直译「表征状态转移」,这是个很拗口的词.我的建议是先不要强行理解,直接看怎么做,等对实施 ...

随机推荐

  1. Yii 获取url 的一些方法

    原文出处http://blog.csdn.net/iefreer/article/details/21325371 1. 获取url中的host信息: Yii::app()->request-& ...

  2. SaltStack部署redis主从

    需求: 一,部署redis主从,一台主一台从 二,redis监听自己的IP地址,而不是0.0.0.0 主:安装,配置,启动 从:安装,配置,启动,主从

  3. android中Logcat的TAG过滤

    如果代码中有这样的log: Log.e("Foo", "error in foo"); Log.d("Foo", "debug i ...

  4. 介绍一个Redis的WEB 客户端

    http://webd.is/ $ git clone git://github.com/nicolasff/webdis.git                $ cd webdis$ make$ ...

  5. 产品开发过程描述xmind

  6. 003-and design-在create-react-app项目中使用antd

    一.概述 create-react-app 是业界最优秀的 React 应用开发工具之一,本文会尝试在 create-react-app 创建的工程中使用 antd 组件,并自定义 webpack 的 ...

  7. POJ1236:Network of Schools(tarjan+缩点)?

    题目: http://poj.org/problem?id=1236 [题意] N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1 ...

  8. 解决升级到Xcode10,react native项目运行报错问题

    今天刚升级到Xcode10,就遇到两个报错问题 错误一:Xcode 10: Build input file double-conversion cannot be found error: Buil ...

  9. Keras实践:模型可视化

    Keras实践:模型可视化 安装Graphviz 官方网址为:http://www.graphviz.org/.我使用的是mac系统,所以我分享一下我使用时遇到的坑. Mac安装时在终端中执行: br ...

  10. Es+kafka搭建日志存储查询系统(设计)

    现在使用的比较常用的日志分析系统有Splunk和Elk,Splunk功能齐全,处理能力强,但是是商用项目,而且收费高.Elk则是Splunk项目的一个开源实现,Elk是ElasticSearch(Es ...