web基础----->jersey整合jetty开发restful应用(一)
这里介绍一个jersey与jetty整合开发restful应用的知识。将过去和羁绊全部丢弃,不要吝惜那为了梦想流下的泪水。
jersey与jetty的整合
一、创建一个maven项目,pom.xml的内容如下
<?xml version="1.0" encoding="UTF-8"?>
<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>com.linux.huhx</groupId>
<artifactId>SpringLearn2</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<jetty-version>8.1.19.v20160209</jetty-version>
</properties> <dependencies>
<!-- jersey的依赖 -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.4</version>
</dependency> <!-- jetty的依赖-->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>${jetty-version}</version>
</dependency> <!-- 对返回json串的支持 -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19.4</version>
</dependency> <!-- junit 测试框架 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> </project>
二、创建一个web容器,在main方法启动jetty
package com.linux.huhx.main; import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; public class HttpServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8090);
ServletHolder servlet = new ServletHolder(ServletContainer.class);
servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
servlet.setInitParameter("com.sun.jersey.config.property.packages", "com.linux.huhx.business");
servlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
handler.addServlet(servlet, "/*");
server.setHandler(handler);
server.start();
System.out.println("start...in 82");
}
}
三、定义restful网格的api接口
package com.linux.huhx.business.action; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map; @Path("hello")
public class HelloHuhxResource { /**
* 简单的get请求,返回json数据
* @return
*/
@GET
@Path("huhx")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> sayHelloHuhx() {
Map<String, String> map = new HashMap<>();
map.put("returnCode", "000000");
map.put("returnMsg", "I love you");
return map;
} /**
* json的请求和json数据的返回
* @param paramMap
* @return
*/
@POST
@Path("linux")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, String> sayHelloLinux(Map<String, String> paramMap) {
Map<String, String> map = new HashMap<>();
map.put("username", paramMap.get("username"));
map.put("password", paramMap.get("password"));
return map;
}
}
四、用浏览器和postman测试的结果如下
- 访问:http://localhost:8090/hello/huhx
返回结果:{"returnCode":"000000","returnMsg":"I love you"}
- 访问:http://localhost:8090/hello/linux 参数:{ "username": "linux", "password": "12345"}
返回结果:{ "password": "12345", "username": "linux"}
友情链接
- 关于jersey的文档:https://jersey.github.io/documentation/latest/index.html
- 关于java EE规范的restful: https://www.safaribooksonline.com/library/view/java-ee-7/9781449370589/ch04.html
web基础----->jersey整合jetty开发restful应用(一)的更多相关文章
- 使用Jersey和Jetty开发RESTful Web service
Jersey RESTful 框架是开源的RESTful框架, 实现了JAX-RS (JSR 311 & JSR 339) 规范,是JAX-RS的参考实现,并且提供了更多的特性和工具,简化了R ...
- ASP.NET Core Web API 开发-RESTful API实现
ASP.NET Core Web API 开发-RESTful API实现 REST 介绍: 符合REST设计风格的Web API称为RESTful API. 具象状态传输(英文:Representa ...
- Web基础开发最核心要解决的问题
Web基础开发要解决的问题,往往也就是那些框架出现的目的 - 要解决问题. 1. 便捷的Db操作: 2. 高效的表单处理: 3. 灵活的Url路由: 4. 合理的代码组织结构: 5. 架构延伸 缓存. ...
- IntelliJ IDEA: maven & jetty 开发 java web
之前使用eclipse + maven + jetty开发java web应用,本着no zuo no gain的想法, 折腾了一下Intellj idea下开发环境的搭建,顺带学习了maven re ...
- eclipse集成jetty开发web项目(不采用maven方式)
以前开发过程部署项目都是采用tomcat,偶然发现jetty,所以试了下,挺方便的,直切主题. 1.下载jetty,楼主使用的jetty8,地址http://download.eclipse.org/ ...
- 使用CXF开发RESTFul服务
相信大家在阅读CXF官方文档(http://cxf.apache.org/docs/index.html)时,总是一知半解.这里向大家推荐一本PacktPub.Apache.CXF.Web.Servi ...
- nodejs 基础篇整合
nodeJs 基础篇整合 最近有朋友也想学习nodeJs相关方面的知识,如果你是后端想接近前端,node作为一门跑在服务端的JS语言从这里入门再好不过了.如果你正好喜欢前端,想走的更高,走的更远.no ...
- Jetty使用教程(四:24-27)—Jetty开发指南
二十四.处理器(Handler ) 24.1 编写一个常用的Handler Jetty的Handler组件用来处理接收到的请求. 很多使用者不需要编写Jetty的Handler ,而是通过使用Serv ...
- Jetty使用教程(四:21-22)—Jetty开发指南
二十一.嵌入式开发 21.1 Jetty嵌入式开发HelloWorld 本章节将提供一些教程,通过Jetty API快速开发嵌入式代码 21.1.1 下载Jetty的jar包 Jetty目前已经把所有 ...
随机推荐
- ubuntu 系统启动异常之无登录界面和版本号启动四个点的地方卡住
zlib 搞的鬼,还没结局,由于rtmpdump 安装需要安装独立zlib库,装完后重启,完了吓一跳,卡住,尼玛这一年的代码都在里面啊!!! ldd /usr/sbin/python 查询库依赖zli ...
- memcached -- 运行memcached
Memcached 运行 Memcached命令的运行: $ /usr/local/memcached/bin/memcached -h 注意:如果使用自动安装, memcached 命令位于 /us ...
- CentOS 6.9编译安装Erlang
转自http://www.jb51.net/article/59823.htm 这篇文章主要介绍了CentOS 6.5源码安装Erlang教程,本文讲解了源码编译安装的过程和遇到的一些错误处理方法,需 ...
- php扩展模块安装
- Hive性能分析和优化方法
Hive性能分析和优化方法 http://wenku.baidu.com/link?url=LVrnj-mD0OB69-eUH-0b2LGzc2SN76hjLVsGfCdYjV8ogyyN-BSja5 ...
- 156 UIImageView 和 CADisplayLink 实现 Tom 汤姆猫动画效果的区别(扩展知识:分组(黄色文件夹)和文件夹引用(蓝色文件夹)区别)
(1)UIImageView 的动画操作,来自定义循环播放动画(不建议使用,内存消耗大) (2)CADisplayLink 是一个计时器,但是同 NSTimer 不同的是,CADisplayLink ...
- java学习之maven
maven是项目构建工具,能把项目抽象成POM(Project Object Model) Maven使用POM对项目进行构建.打包.文档化等操作 解决了项目需要类库的依赖管理,简化了项目开发环境搭建 ...
- Innodb表空间
Innodb有两种管理表空间的方法 独立表空间:每一张表都会生成独立的文件来进行存储,每一张表都有一个.frm表描述文件,和一个.ibd文件.其中ibd文件包括了单独一个表的数据内容和索引内容. 共享 ...
- 检出商品详情中的图片并替换url
原有的批量导入是按照系统本身的功能导入商品,现在需要用接口将图片上传图片服务器 所以需要将批量导入的商品图片取出来,上传后替换掉原来的url (1)检出详情中的图片,用文件名做key private ...
- scala中获取Map中key和value的方法
val scores=Map("Alice"->10,"Bob"->3,"Cindy"->8) // 获取所有的key v ...