您当前的位置: 首页 >  restful

_waylau

暂无认证

  • 5浏览

    0关注

    275博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

用Jersey构建RESTful服务1--HelloWorld

_waylau 发布时间:2014-03-17 15:43:00 ,浏览量:5

一、环境 1、Eclipse Juno R2 2. Tomcat 7 3. Jersey 2.7 下载地址(https://jersey.java.net/download.html) 二、流程 1.Eclipse 中创建一个 Dynamic Web Project ,本例为“RestDemo”

2.按个各人习惯建好包,本例为“com.waylau.rest.resources”

3.解压jaxrs-ri-2.7,

将api、ext、lib文件夹下的jar包都放到项目的lib下;

项目引入jar包

4.在resources包下建一个class“HelloResource”
package com.waylau.rest.resources;


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;


@Path("/hello")
public class HelloResource {
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String sayHello() {
		return "Hello World!" ;
	}
 
	
	@GET
	@Path("/{param}")  
	@Produces("text/plain;charset=UTF-8")
	public String sayHelloToUTF8(@PathParam("param") String username) {
		return "Hello " + username;
	}
	
}
5.修改web.xml,添加基于Servlet-的部署
   
     Way REST Service
	 org.glassfish.jersey.servlet.ServletContainer
         
		jersey.config.server.provider.packages
      	com.waylau.rest.resources
   	   
    1
  
  
  
    Way REST Service
    /rest/*
  
6.项目部署到tomcat,运行 7.浏览器输入要访问的uri地址 http://localhost:8089/RestDemo/rest/hello

输出Hello World!

http://localhost:8089/RestDemo/rest/hello/Way你好吗 输出Hello Way你好吗 参考:https://jersey.java.net/documentation/latest/user-guide.html
关注
打赏
1651845987
查看更多评论
立即登录/注册

微信扫码登录

1.1076s