[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,希望能够做成 ...
随机推荐
- QT VS检测内存泄漏
测试程序:http://download.csdn.net/detail/ajaxhe/4085447 vld-2.2.3: http://vld.codeplex.com/releases/view ...
- page分页
首先封装一个分页类 public class Page<T> { /** * 当前页号 */ private int pageNumber; /** * 总条数 */ private in ...
- switf资源
http://www.swiftv.cn/ http://letsswift.com/
- BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
题目 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 53 Solve ...
- BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...
- mysql中怎样查看和删除唯一索引
mysql中怎样查看和删除唯一索引. 查看唯一索引: show index from mytable;//mytable 是表名 查询结果例如以下: 查询到唯一索引后,怎样删除唯一索引呢,使用例如以下 ...
- MySQL 讨厌哪种类型的查询
㈠ 任何查询都讨厌 只要是查询.MySQL都讨厌.执行查询最快的方式就是不去执行 缓存为王.比如Redis或者memcache ㈡ 查 ...
- C与C++ 无参函数的区别
在<C++ 编程思想>:“关于无参函数声明,C与C++有很大的差别.在C语言中,声明int fun1(),意味着一个可以有任意数目和类型的函数:而在C++中,指的却是一个没有参数的函数”. ...
- 设置outlook自动回复
当有同事需要出差时,或者不能即时回复邮件时,可用此功能. 下面列出设置步骤: 1. 首先,在桌面新建一个用以保存模板的文件夹,例如:emaii. 2. 新建一封邮件,输入你要自动回复的内容.另存为 ...
- php数组操作小结
$x unset($x[3]) //key不重排