[REST Jersey] @QueryParam Demo
This demo sourced from the jersey tutor. https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e1433 provides the necessary info for you.
HelloWorldResource.java
package com.example; import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
/**
*
* @author
*/
@Path("helloworld")
public class HelloWorldResource {
public static final String CLICHED_MESSAGE = "Hello World!";
/*
@GET
@Produces("text/plain")
public String getHello() {
return CLICHED_MESSAGE;
}
*/
@GET
@Path("/get")
@Produces("text/plain")
//@Produces("application/json")
public String getQueryObj(
@QueryParam ("featureID") @DefaultValue("0") String fid,
@QueryParam ("userID") @DefaultValue( "0" ) int uid,
@QueryParam ("color") @DefaultValue("red") ColorParam clr
// color default value: green, blue
) {
return new String("QueryParam: {"+ fid + ": " + uid + " : " + clr + "}." + CLICHED_MESSAGE);
} }
ColorParam.java
package com.example;
import java.awt.Color;
import javax.ws.rs.WebApplicationException;
import java.lang.reflect.Field; public class ColorParam extends Color { public ColorParam(String s) {
super(getRGB(s));
} private static int getRGB(String s) {
if (s.charAt(0) == '#') {
try {
Color c = Color.decode("0x" + s.substring(1));
return c.getRGB();
} catch (NumberFormatException e) {
throw new WebApplicationException(400);
}
} else {
try {
Field f = Color.class.getField(s);
return ((Color)f.get(null)).getRGB();
} catch (Exception e) {
throw new WebApplicationException(400);
}
}
}
}
Call it after the above java files are packaged as a war deployed in tomcat container.
http://hostname:8080/simple-service-webapp/webapi/helloworld/get?featureID=12
QueryParam: {12: 0 : com.example.ColorParam[r=255,g=0,b=0]}.Hello World! http://hostname:8080/simple-service-webapp/webapi/helloworld/get?featureID=12&userID=123
QueryParam: {12: 123 : com.example.ColorParam[r=255,g=0,b=0]}.Hello World! http://hostname:8080/simple-service-webapp/webapi/helloworld/get?featureID=12&userID=123&color=yellow
QueryParam: {12: 123 : com.example.ColorParam[r=255,g=255,b=0]}.Hello World!
[REST Jersey] @QueryParam Demo的更多相关文章
- [接口服务] Jersey Rest Demo
http://files.cnblogs.com/files/avivaye/RestProject.rar
- jersey构建rest服务返回json数据
1. eclipse 创建 dynamic web project 2. 将jersey相关jar包放到libs目录下 3. web.xml 增加 jersey 相关内容 <?xml ver ...
- 通过mybatis读取数据库数据并提供rest接口访问
1 mysql 创建数据库脚本 -- phpMyAdmin SQL Dump -- version 4.2.11 -- http://www.phpmyadmin.net -- -- Host: lo ...
- Jersey RESTful WebService框架学习(三)使用@QueryParam
介绍:@QueryParamuri路径请求参数写在方法的参数中,获得请求路径附带的参数.比如:@QueryParam("desc") String desc 前端控制 <!D ...
- Spring集成Jersey开发(附demo)
下文将会初步介绍如何在Spring中集成Jersey,并附简单的demo 所依赖的技术版本: Jersey 1.8 Spring 3.0.5.RELEASE 1. 项目依赖 pom.xml定义(注意去 ...
- Jersey 写restful接口时QueryParam ,FormParam 等的区别
今天用jersey写接口,发现有一个post方法中没有得到参数,查了半天发现自己一不小心将@formparam写成了@queryparam,真是一个悲伤的故事.在这里把几个参数类型整理了一下放出来. ...
- jersey简单总结与demo
参考链接:https://www.iteye.com/blog/dyygusi-2148029?from=singlemessage&isappinstalled=0 测试代码: https: ...
- Jersey的RESTful简单案例demo
REST基础概念: 在REST中的一切都被认为是一种资源. 每个资源由URI标识. 使用统一的接口.处理资源使用POST,GET,PUT,DELETE操作类似创建,读取,更新和删除(CRUD)操作. ...
- Java完成最简单的WebService创建及使用(REST方式,Jersey框架)
前言: 一直以来都对WebService感兴趣,但因为难以理解WebService到底是什么,所以了解甚少.周二的时候有个跟我关系比较好的同事想要自己写个WebService的小Demo,希望能够做成 ...
随机推荐
- 导出Excel并下载,但无法定制样式的方法!
拿来的,望原创见谅! public void EXCELDown(DataTable dt, string strFileName) { Response.ContentEncoding = Syst ...
- C++ ofstream和ifstream详细用法
转载地址:http://soft.chinabyte.com/database/460/11433960.shtml ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就 ...
- 解题报告 HDU1176 免费馅饼
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Windows 8.1 RTM初体验
Windows 8.1正式发布在10月17日,现在可以在MSDN/TechNet进行订阅下载. 操作系统版本号和Windows Server 2012 R2保持一致. 开始屏幕动态磁贴现在有4种尺寸可 ...
- ceph存储之ceph客户端
CEPH客户端: 大多数Ceph用户不会直接往Ceph存储集群里存储对象,他们通常会选择Ceph块设备.Ceph文件系统.Ceph对象存储之中的一个或多个: 块设备: 要实践本手册,你必须先完成存储集 ...
- ThinkPHP - 每个操作都检测用户是否登录
TP提供了一个自动执行的函数_initialize(), 你创建一个公共控制器CommonAction.class.php文件. 定义了此方法,不能存在构造方法__construct() <?p ...
- CMarkUp接口说明
CMarkup是一个小型XML的分析器,实现语言是C++,英文版的接口说明地址为:http://www.firstobject.com/dn_markupmethods.htm 有厉害的网友已经翻译出 ...
- 2014-7 Andrew Ng 自动化所报告听后感
原文:http://blog.sina.com.cn/s/blog_593af2a70102uwhl.html 一早出发,8点20就赶到现场, 人越聚越多,Ng提前几分钟到达现场,掌声一片. N ...
- JNI之HelloWorld
什么是JNI? JNI java本地开发接口 JNI 是一个协议 这个协议用来沟通java代码和外部的本地代码(c/c++). 通过这个协议,java代码就可以调用外部的c/c++代码 外部的c/c+ ...
- Hadoop Error:Name node is in safe mode的解决方法
Error:name node is in safe mode 解决方法:hadoop dfsadmin -safemode leave(见图)