Struts2写接口返回json数据

公司老项目Struts,写了一些接口的方法,记录下来。

第一种:

struts.xml配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC    
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 该 属性指定Struts 2中的action由spring容器创 建 -->   
	<constant name="struts.objectFactory" value="spring" />
	<package name="ajax" extends="json-default" namespace="/">
		<action name="testAjaxJson" class="StudentAction">
		    <result type="json">
		        <param name="root">ajaxResult</param>
		    </result>
		</action>
	</package>	
</struts>

Action:

package com.mangocity.ship.web.action;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport {
	private JSONObject ajaxResult;

	public String queryStudent() {
		JSONObject json = new JSONObject();
		json.put("code", "00");
		json.put("Message", "Success");
		return SUCCESS;
	}

	public JSONObject getAjaxResult() {
		return ajaxResult;
	}

	public void setAjaxResult(JSONObject ajaxResult) {
		this.ajaxResult = ajaxResult;
	}

}

访问地址http://localhost:8080/TestAjax/testAjaxJson!queryStudent.shtml

第二种:

struts.xml配置文件像平常返回页面一样配置,但是注意返回的jsp文件的内容如下:

<%@ page contentType="text/html;charset=utf-8"%>${rsJson}

其中用el表达式获取action中的rsJson属性值,这个属性就是接口要返回的数据。

这种方式只是在公司内网各个业务线使用的接口,如果是对外的不建议这样使用。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接口接受json数据的过程如下所示:首先,通过post请求将json数据传递到接口的url中。在接口中,可以使用类似于form表单的方式获取post请求体中的内容。具体的代码如下所示: ```java // 获取请求体中内容,转换为json JSONObject jsonRequest = JSONObject.fromObject(this.getStrResponse()); // 获取json中的参数 String accountId1 = jsonRequest.getString("account_id"); Long accountId = Long.parseLong(accountId1); String verifyContent = jsonRequest.getString("verify_content"); String verifyType = jsonRequest.getString("verify_type"); // 对参数进行处理 if ("N".equalsIgnoreCase(verifyType)) { verifyType = "1"; } else if ("A".equalsIgnoreCase(verifyType)) { verifyType = "2"; } else if ("P".equalsIgnoreCase(verifyType)) { verifyType = "3"; } // 进行具体的业务处理 // ... // 返回结果 return SUCCESS; ``` 在上述代码中,首先将post请求体中的内容转换为json对象。然后,通过json对象获取相应的参数值。接下来,对参数进行处理,例如将字符串类型的参数转换为其他类型。最后,进行具体的业务处理,并返回相应的结果。 #### 引用[.reference_title] - *1* *3* [使用Struts2接口实现接收json格式数据](https://blog.csdn.net/Devin_ran/article/details/53639205)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用接口接收json数据](https://blog.csdn.net/qq_36144386/article/details/114013086)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值