我正在尝试从Apache Struts 2中的JSON请求获取数据。
我已经使用此URL测试: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,我想知道我可以使用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>
您需要做2件事:
1.-一旦从URL获取字符串(您的结果变量),您需要将其施放到对象中,例如,您可以创建一个称为响应的Java Bean,此对象看起来像这样:
public class Response{
private String time;
private Long milisenconds;
private Date date;
//setters and getters
}
2.现在,您可以使用Jackson-Mapper库将字符串转换为响应对象,看起来像这样:
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.-因此,现在您可以调用此URL: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)JAVA脚本解析JSON字符串并显示值
或
2)Java脚本库(如Angularjs和jQuery)具有内置的支持,可以轻松有效地使用最小的代码来实现它们。
两件事:
1)创建对象的Javabean。Javabean中字段的名称必须与JSON的字段相匹配(尽管需要时可以解决此问题)。
public class JsonTest{
private String time;
private Long milliseconds_since_epoch;
private Date date;
//setters and getters
}
2)使用GSON将JSON搅拌到一个物体中。
public static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
然后使用它...
JsonTest object = gson.fromJson(jsonString, JsonTest.class);
从任何对象获取JSON字符串..
String json = gson.toJson(someObject);