我正在尝试从Apache Struts 2中的JSON请求获取数据
我已经使用此网址进行了测试:http://date.jsontest.com/
在StrutsJsonClientAction.java
文件中,行
System.out.println(result);
将其打印到控制台:
{ "time": "12:04:12 PM", "milliseconds_since_epoch": 1431086652240, "date": "05-08-2015"}
我想知道如何在result.jsp
今天,我发现resttemplate可以在春天使用,我想知道struts2中是否有像restemplate这样的工具可供使用?
我目前的解决方案:StrutsJsonClientAction.java
package com.example.action;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class StrutsJsonClientAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private final String url="http://date.jsontest.com/";
public String execute() throws Exception {
HttpServletRequest request= ServletActionContext.getRequest();
request.setCharacterEncoding("utf-8");
URL myUrl = new URL(url);
//System.out.println(myUrl.toString());
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String result="";
String temp=null;
while ((temp=reader.readLine())!= null) {
result=result+temp;
}
System.out.println(result);
return SUCCESS;
}
}
档案struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="GoToGetApi" class="com.example.action.StrutsJsonClientAction">
<result>/resault.jsp</result>
</action>
</package>
</struts>
答案 0 :(得分:1)
你需要做两件事:
1.-一旦你从URL(你的结果变量)得到String你需要转换成一个Object,例如,你可以创建一个名为Response的Java Bean,这个对象将如下所示:
public class Response{
private String time;
private Long milisenconds;
private Date date;
//setters and getters
}
2.现在你可以使用jackson-mapper库转换你的字符串转换为Response对象,如下所示:
private Response response;
//don't forget to add your response and the getter to be able to show this value in your jsp, otherwise you can't do it
public Response getResponse(){
return response;
}
public String execute(){
//your code to get your String result value.
ObjectMapper mapper=new ObjectMapper();
response=mapper.readValue(result,Response.class);//will parse your String "result" into Response object using this library
return SUCCESS;
}
3. - 您还需要在项目中添加新库以支持JSON响应,您可以找到此库:struts2-json-plugin
在你的包/动作配置中,不是返回一个页面,你将返回一个json类型:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="GoToGetApi" class="com.example.action.StrutsJsonClientAction">
<result type="json">
<param name="root">response</param>
</result>
</action>
</package>
</struts>
4.-所以现在你可以调用这个网址:http://localhost:8080/yourProyect/GoToGetApi.action,你会看到json格式的响应。
但是如果你只有一个在jsp中“打印”json字符串,你可以在你的动作类中添加它:
private String apiResponse;
//get of apiResponse
public String execute(){
//your current code here
System.out.println(result);
apiResponse=result;
return SUCCESS;
}
在你的jsp中你只需要添加:
<body>
<!--your current code here-->
<label>This is my response:</label> ${apiResponse}
<br/>
<label>This is my response:</label><s:property value="apiResponse" />
</body>
</html>
答案 1 :(得分:0)
答案 2 :(得分:0)
两件事:
1)创建对象的JavaBean。 JavaBean中字段的名称必须与JSON的字段匹配(尽管如果需要,您可以绕过它)。
public class JsonTest{
private String time;
private Long milliseconds_since_epoch;
private Date date;
//setters and getters
}
2)使用Gson将JSON Stirng编组到一个对象中。
public static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
然后使用它......
JsonTest object = gson.fromJson(jsonString, JsonTest.class);
从任何对象获取JSON字符串....
String json = gson.toJson(someObject);