servlet+JSP与SpringBoot+Vue项目交互——servlet请求SpringBoot接口

问题

servlet+JSP与SpringBoot+Vue项目交互——servlet请求SpringBoot接口

详细问题

笔者前一段时间开发一个项目,使用的技术框架是servlet+JSP,现阶段开发的项目技术框架为SpringBoot+Vue,笔者现在需要输入servlet+JSP请求SpringBoot接口,获取接口相应,如何实现

解决方案

1、若读者使用的是 Maven 作为构建工具,您可以在项目的 pom.xml 文件中添加以下依赖项:

Copy code
<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.4</version>
    </dependency>
</dependencies>

若读者使用的是 Gradle 作为构建工具,您可以在项目的 build.gradle 文件中添加以下依赖项:

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.4'
}

2 、创建该工具类

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class RequestUtils {

    public static String sendGetRequest(String url) throws IOException {
        // 创建HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();
        // 创建HttpGet对象,设置接口地址
        HttpGet httpGet = new HttpGet(url);

        // 发送请求并获取响应
        HttpResponse response = httpClient.execute(httpGet);
        // 获取响应码
        int statusCode = response.getStatusLine().getStatusCode();
        // 获取响应内容
        HttpEntity entity = response.getEntity();
        String responseBody = EntityUtils.toString(entity);

        // 输出响应信息
        System.out.println("Response Code: " + statusCode);
        System.out.println("Response Body: " + responseBody);

        return responseBody;
    }

    public static String sendPostRequest(String url, String requestBody) throws IOException {
        // 创建HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();
        // 创建HttpPost对象,设置接口地址
        HttpPost httpPost = new HttpPost(url);
        // 设置请求头信息
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.setHeader("Accept", "application/json");
        // 设置请求体内容
        httpPost.setEntity(new StringEntity(requestBody));

        // 发送请求并获取响应
        HttpResponse response = httpClient.execute(httpPost);
        // 获取响应码
        int statusCode = response.getStatusLine().getStatusCode();
        // 获取响应内容
        HttpEntity entity = response.getEntity();
        String responseBody = EntityUtils.toString(entity);

        // 输出响应信息
        System.out.println("Response Code: " + statusCode);
        System.out.println("Response Body: " + responseBody);

        return responseBody;
    }

    public static void main(String[] args) throws IOException {
        String getUrl = "http://localhost:8081/data";  // 替换为您的 GET 请求接口地址
        String postUrl = "http://localhost:8081/login";  // 替换为您的 POST 请求接口地址
        String requestBody = "username=test&password=123456";  // 替换为 POST 请求的请求体内容

        String getResponse = sendGetRequest(getUrl);
        // 在这里可以对 GET 请求的响应进行处理

        String postResponse = sendPostRequest(postUrl, requestBody);
        // 在这里可以对 POST 请求的响应进行处理
    }
}

问题原因

原先使用 Servlet 和 JSP 技术框架开发的项目迁移到使用 Spring Boot 和 Vue 技术框架的项目。笔者不了解如何在新的技术框架下,通过 Servlet 和 JSP 发起请求并获取 Spring Boot 接口的响应。

解决原因

通过封装一个请求工具类,您可以在新的技术框架下方便地发送 GET 和 POST 请求,并获取 Spring Boot 接口的响应。该工具类使用 Apache HttpClient 库来处理 HTTP 请求和响应,提供了发送 GET 和 POST 请求的方法。

参考文献

工具类代码实现参考chatgpt

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞滕人生TYF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值