注明:jersey-2.10.1需要JDK7

1、构建Maven工程,引入相关的依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rest</groupId>
<artifactId>rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
</project>

2、编写启动Jersey服务类

package com.rest.panguoyuan.test;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
public class Server {
private static final URI BASE_URI = URI.create("http://10.71.197.201:8088/basepath/");
public static void main(String[] args) {
try {
<span style="white-space:pre"> </span>           System.out.println("\"Hello World\" Jersey Example App");
<span style="white-space:pre"> </span>           final ResourceConfig resourceConfig = new ResourceConfig(ServerImpl.class);
<span style="white-space:pre"> </span>           final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig);
<span style="white-space:pre"> </span>           System.out.println(String.format("Application started.\nTry out %s%s\nHit enter to stop it...",
<span style="white-space:pre"> </span>                    BASE_URI, "classpath/methodpath/parameters"));
<span style="white-space:pre"> </span>           System.in.read();
<span style="white-space:pre"> </span>           server.shutdown();
} catch (Exception ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

3、编写提供外部访问的服务类

package com.rest.panguoyuan.test;
import java.io.UnsupportedEncodingException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.google.gson.Gson;
@Path("classpath")
public class ServerImpl {
@GET
@Path("methodpath/{userId}")
@Produces("text/plain")
public String getUser(@PathParam("userId") int userId) {
User doc = new User("panguoyuan",90);
Gson gson = new Gson();
String jsonDoc = gson.toJson(doc);
System.out.println("userId="+userId);
return jsonDoc;
}
}

4、相关类(User)

package com.rest.panguoyuan.test;
public class User{
private String name;
private int age;
public User() {}
public User(String name,int age) {this.name = name;    this.age = age;}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

最简单的Rest服务相关推荐

  1. Docker安装Apache与运行简单的web服务——httpd helloworld

    Docker运行简单的web服务--httpd helloworld目录[阅读时间:约5分钟] 一.Docker简介 二.Docker的安装与配置[CentOS环境] 三.Docker运行简单的web ...

  2. SpringCloud(第 002 篇)简单电影微服务类(消费方,而提供方为用户微服务)

    2019独角兽企业重金招聘Python工程师标准>>> SpringCloud(第 002 篇)简单电影微服务类(消费方,而提供方为用户微服务) 一.大致介绍 微服务与微服务之间通过 ...

  3. [WS]使用Axis发布简单的Web服务

    使用Axis,要发布一个Web服务非常简单,简直不能再简单了,尽管看起来过程和相关代码有些长.我这个帖子里用到了这些软件:Axis 1.1.Eclipse 2.1和Eclipse的Tomcat插件2. ...

  4. 使用Axis2创建一个简单的WebService服务

    使用过Java进行过WebService开发都会听过或者接触过Apache Axis2,Axis2框架是应用最广泛的WebService框架之一了. 这里使用Axis2来开发和部署一个最简单的WebS ...

  5. 通过HttpListener实现简单的Http服务

    通过HttpListener实现简单的Http服务 基本概念 HttpListener提供一个简单的.可通过编程方式控制的 HTTP 协议侦听器.通过它可以很容易的提供一些Http服务,而无需启动II ...

  6. SpringCloud(第 054 篇)简单 Quartz-Cluster 微服务,采用注解配置 Quartz 分布式集群...

    2019独角兽企业重金招聘Python工程师标准>>> SpringCloud(第 054 篇)简单 Quartz-Cluster 微服务,采用注解配置 Quartz 分布式集群 一 ...

  7. .NET 搭建简单的通知服务

    搭建简单的通知服务 Intro 很多情况下,我们都会遇到一些需要进行通知报警的场景,比如说服务器资源监控报警,抢到火车票后通知用户进行付款. 原来主要是用的钉钉群里的机器人来做的通知,周末看到原来做 ...

  8. 构建简单的微服务架构

    前言 本篇仅作引导,内容较多,如果阅读不方便,可以使用电脑打开我们的文档官网进行阅读.如下图所示: 文档官网地址:docs.xin-lai.com 目录 总体介绍   微服务架构的好处    微服务架 ...

  9. 基于消息中间件RabbitMQ实现简单的RPC服务

    转载自  基于消息中间件RabbitMQ实现简单的RPC服务 RPC(Remote Procedure Call,远程过程调用),是一种计算机通信协议.对于两台机器而言,就是A服务器上的应用程序调用B ...

  10. 使用SpringBoot搭建一个简单的webSocket服务

    前言 个人地址:使用SpringBoot搭建一个简单的webSocket服务 什么是WebSocket? WebSocket是一个HTML5新增的协议,它的目的在浏览器和服务器之间建立一个不受限的双向 ...

最新文章

  1. 【iOS】通讯录分组方式展示数据
  2. 数据结构之排序算法(C语言)
  3. springboot 做表白墙_华农表白墙144期 | 等什么时候,她从我开的花店前经过,我把整个花店送个她可好。...
  4. java序列化_技术干货 | JAVA反序列化漏洞
  5. Java03接口与内部类
  6. rx580网络适配器下载_通过Rx和数据绑定简化RecyclerView适配器
  7. 国家哲学社会科学文献中心
  8. 再也不用装双系统和虚拟机了!——吐血推荐令Windows与Liunx合二为一的WSL2
  9. bzoj 3329: Xorequ(DP+矩阵快速幂)
  10. JVM内存:年轻代,老年代,永久代
  11. linux查看udp丢包数量,Linux下UDP丢包问题分析思路
  12. 群面时被问到“让你淘汰一个组员”一般如何淘汰谁?
  13. STM32HAL库串口处理---中断收发
  14. @31省区市高考时间表来了,祝考试成功
  15. 一个岔路口分别通向诚实国和说谎国。 来了两个人,已知一个是诚实国的,另一个是说谎国的。 诚实国永远说实话,说谎国永远说谎话。现在你要去说谎国, 但不知道应该走哪条路,需要问这两个人。请问应该怎么问?(
  16. Real-Time Rendering Fourth Edition读书笔记1
  17. Dockerfile构建镜像最佳实践
  18. HLW8112(8110)在电源转化效率系统的应用
  19. 各类文件对应Content-Type
  20. 分布式链路追踪深入了解

热门文章

  1. Java学习需要多长时间?
  2. 史上最全的ocr文字识别体验,让你一次用个够
  3. 统计学 常用的数据分析方法大总结,推荐收藏
  4. 2017济南北大青鸟accp和学士后课程的真实情况
  5. VMPlayer安装
  6. 【头像变更】自己瞎做一个头像,放真实头像做头像虽然真诚,但是心里实在不想!
  7. Linux 计算器程序
  8. mysql函数大全之数字函数
  9. matlab差分法解拉普拉斯方程,拉普拉斯方程有限差分法的MATLAB实现
  10. 【换脸软件DFL2.0官方使用手册详解】