RestEasy编写API

本文介绍如何使用RESTEASY实现RESTful API接口,包括处理图片请求、下载并保存图片,以及通过GET方法返回处理结果。文章详细展示了从部署依赖、实现API接口到web.xml配置的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我是从这篇文章学习的RESTEASY
https://www.cnblogs.com/langtianya/p/7624647.html

1. 现实需求

任务:发送请求给服务器,解析请求,返回结果
请求内容:user_id, 图片

2. 分析

1)首先要指定url,参数列表,请求方式,提供一个对外开放接口;
2)业务逻辑在接口中,返回数据;
3) 调用方会根据提供的接口url,指定的参数列表,请求方式post/get调用接口。

3. 实现过程

1) pom.xml部署依赖
	<dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <version>3.0.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.19.Final</version>
        <!--<scope>provided</scope>-->
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.0.19.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-multipart-provider</artifactId>
        <version>3.0.19.Final</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>3.0.19.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-servlet-initializer</artifactId>
        <version>3.0.19.Final</version>
    </dependency>
2)实现API接口 TestRest

思路
图片url是:“http://pic38.nipic.com/” +id+"/"+pic_name
得到一张图片
将图片另存到本地,并得到以时间戳命名的名称
返回fin = pic + id

代码:

package Rest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

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

/**
 * 用来处理请求的API
 * @author bobo
 *
 */
@Path("/path1")
public class TestRest {
	@GET
	@Produces("application/json")
	@Path("subpath/{id}/{pic_name}")
	public String get(@PathParam("id") String id , @PathParam("pic_name") String pic_name) throws IOException{
		//String result = "接收参数: " + id +"  " + pic_name;
		URL url1 = new URL("http://pic38.nipic.com/" +id+"/"+pic_name);
		URLConnection uc = url1.openConnection();
        InputStream inputStream = uc.getInputStream();
        String path = "D:\\image\\"+System.currentTimeMillis()+".jpg";
        FileOutputStream out = new FileOutputStream(path);
        
        int j = 0;
        while ((j = inputStream.read()) != -1) {
            out.write(j);
        }
        inputStream.close();
        
        //得到pic
        String pic = path.substring(9, path.length()-4);
        String fin = pic + " " + id;
        System.out.println(pic + " " + id);
		
		return fin;
	}
}

测试:
运行程序,tomcat启动
输入url http://localhost:8080/api_test_1/path1/subpath/20140225/2531170_214014788000_2.jpg
返回:
在这里插入图片描述

3) web.xml配置
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
      <param-name>resteasy.resources</param-name>
      <param-value>Rest.TestRest</param-value>
  </context-param>
  <listener>
      <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
  </listener>
  <servlet>
      <servlet-name>Resteasy</servlet-name>
      <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>Resteasy</servlet-name>
      <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

4)测试API接口 TestRestAPI

代码:

package Rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
  
public class TestRestAPI{  
    public static final String USER_API =   
        "http://localhost:8080/api_2/path1/subpath/20140225/2531170_214014788000_2.jpg";  
    public static String getPic() throws IOException{
    	//列出原始数据
        StringBuilder json = new StringBuilder();   
        
    	URL url = new URL(USER_API);  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        
        connection.setRequestMethod("GET");  
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
        connection.setConnectTimeout(1000);  
        
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); 
        
        String inputLine = null; 
        
        while ( (inputLine = in.readLine()) != null){ 
            json.append(inputLine); 
        } 
        
        String Strjson=json.toString();
        return Strjson.toString().split(" ")[0];
    }
    public static String getUid() throws IOException{
    	//列出原始数据
        StringBuilder json = new StringBuilder();   
        
    	URL url = new URL(USER_API);  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        connection.setRequestMethod("GET");  
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
        connection.setConnectTimeout(1000);  
        
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); 
        
        String inputLine = null; 
        
        while ( (inputLine = in.readLine()) != null){ 
            json.append(inputLine); 
        } 
        
        String Strjson=json.toString();
        return Strjson.toString().split(" ")[1];
    }
    public static void main(String[] args) throws IOException {
    	//列出原始数据
        StringBuilder json = new StringBuilder();   
        
    	URL url = new URL(USER_API);  
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
        connection.setRequestMethod("GET");  
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
        connection.setConnectTimeout(1000);  
       
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); 
        
        String inputLine = null; 
        
        while ( (inputLine = in.readLine()) != null){ 
            json.append(inputLine); 
        } 
        String Strjson=json.toString();
        String[] str = Strjson.toString().split(" ");
        String pic_name = str[0];
        String id = str[1];
        int user_id = Integer.parseInt(id);
        System.out.println("pic_name:");
        System.out.println(pic_name); 
        System.out.println("id:"); 
        System.out.println(user_id);
  
	}
} 

测试:
运行程序,tomcat启动
给定url
http://localhost:8080/api_test_1/path1/subpath/20140225/2531170_214014788000_2.jpg
两个方法:
getPic() 得到:1571794041366
getUid() 得到:20140225
主函数得到:
pic_name:
1571794041366
id:
20140225

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值