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!

最新文章

  1. 博客代码美化(SyntaxHighlighter)
  2. 开源库Magicodes.WeChat.SDK总体介绍
  3. centos安装firefox flash插件
  4. OpenGL®ES基础
  5. Compilation failed: this version of PCRE is not compiled with PCRE_UTF8 support at offset 0
  6. MySQL中内存分为全局内存和线程内存
  7. C#发送简单的HTTP POST请求给传统的ASP网页。
  8. mysql 开启记录慢查询记录
  9. lightoj 1031 区间dp
  10. MySQL主键添加/删除
  11. 微软推荐的130道ASP.NET常见面试题及答案
  12. 关于jdbc注冊驱动的那点事
  13. New UWP Community Toolkit - RotatorTile
  14. 查看CPU使用率
  15. Cookie的使用(14)
  16. 关于"Could not find acceptable representation"错误
  17. HBase读取与写入流程
  18. 如何简单实现接口自动化测试(基于 python) 原博主地址https://blog.csdn.net/gitchat/article/details/77849725
  19. asp.net json,对象,字符串的相互转换
  20. 免安装版本Mysql配置

热门文章

  1. Xamarin.Android开发实践(四)
  2. F#实现的单链表(函数式的思想)
  3. Ajax学习(三)——XMLHttpRequest对象的五步使使用方法
  4. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
  5. ceph install
  6. iOS 开发http post 文件的上传
  7. 安装 unixbench 报错解决方法
  8. Java--再次理解多态
  9. github过程中收集
  10. Raphael Js矢量库API简介: