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. Powerdesigner逆向工程从sql server数据库生成pdm【转】

    Powerdesigner逆向工程从sql server数据库生成pdm 第一步:打开"控制面板"中的"管理工具" 第二步:点击"管理工具" ...

  2. MVC之Filter

    过滤器的理解 Filter就是过滤器,在WebForm中,各种管道事件就是相当于过滤器,在MVC中,过滤器是单独的一种机制,分为方法过滤器和异常处理过滤器,方法过滤器实现的功能是在执行某一个请求得方法 ...

  3. android使用library

      http://www.vogella.com/tutorials/AndroidLibraryProjects/article.html     介绍support lib使用 http://de ...

  4. php5.6 的curl开启和之前的不太一样了,搞了半天,记录下

    设置电脑环境变量中的->系统变量(注:不是用户变量) 新建 PHP_HOME 值 D:\php /PHP 根目录 Path 增加 ;%PHP_HOME%;%PHP_HOME%\ext       ...

  5. 服务器为什么这么慢?耗尽了CPU、RAM和磁盘I/O资源

    机器运行缓慢通常是由于消耗了太多系统特定的资源.系统的主要资源包括CPU.RAM.磁盘I/O以及网络.过度使用这些资源的任何一种都会让系统陷入困境.不过,如果能登录到系统之中,可以借助大量工具确定问题 ...

  6. uva 11105 - Semi-prime H-numbers(数论)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36644069 option=com_onli ...

  7. nodejs通过代理(proxy)发送http请求(request)

    有可能有这样的需求,需要node作为web服务器通过另外一台http/https代理服务器发http或者https请求,废话不多说直接上代码大家都懂的: var http = require('htt ...

  8. 10.numpy基本用法

    参考: https://blog.csdn.net/sinat_32547403/article/details/54017551

  9. MyBatis—mybatis-config.xml配置介绍

    在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码   说明: 收藏代码 1.  <bean id="sqlSessionFactory" ...

  10. android,结合Timer和TimerTask实现定时任务

    当我们需要每隔一段时间执行一个任务的时候,就需要使用TimerTask了,下面是入门的例子, 值得注意的是Timer.TimerTask,cancel之后就需要重新声明一个对象,否则会报错的哦~ pa ...