最简单的Rest服务

注明:jersey-2.10.1需要JDK7

1、构建Maven工程,引入相关的依赖

<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>rest</groupId>
  <artifactId>rest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>rest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
		<dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.2.4</version>
		</dependency>
        
	</dependencies>
</project>


2、编写启动Jersey服务类

package com.rest.panguoyuan.test;


import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;


import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;


public class Server {


    private static final URI BASE_URI = URI.create("http://10.71.197.201:8088/basepath/");


    public static void main(String[] args) {
        try {
<span style="white-space:pre">	</span>           System.out.println("\"Hello World\" Jersey Example App");
<span style="white-space:pre">	</span>           final ResourceConfig resourceConfig = new ResourceConfig(ServerImpl.class);
<span style="white-space:pre">	</span>           final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig);
<span style="white-space:pre">	</span>           System.out.println(String.format("Application started.\nTry out %s%s\nHit enter to stop it...",
<span style="white-space:pre">	</span>                    BASE_URI, "classpath/methodpath/parameters"));
<span style="white-space:pre">	</span>           System.in.read();
<span style="white-space:pre">	</span>           server.shutdown();
        } catch (Exception ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}
3、编写提供外部访问的服务类

package com.rest.panguoyuan.test;

import java.io.UnsupportedEncodingException;

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

import com.google.gson.Gson;


@Path("classpath")
public class ServerImpl {

    @GET
    @Path("methodpath/{userId}")
    @Produces("text/plain")
    public String getUser(@PathParam("userId") int userId) {
    	User doc = new User("panguoyuan",90);
    	Gson gson = new Gson();
    	String jsonDoc = gson.toJson(doc);
    	System.out.println("userId="+userId);
        return jsonDoc;
    }
}
4、相关类(User)
package com.rest.panguoyuan.test;

public class User{
	private String name;
	private int age;
	
	public User() {}
	public User(String name,int age) {this.name = name;	this.age = age;}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值