Reputation: 6693
I'm trying to get data from a JSON request in Apache Struts 2.
I've tested with this url: http://date.jsontest.com/.
In StrutsJsonClientAction.java
file, line
System.out.println(result);
prints this to the console:
{ "time": "12:04:12 PM", "milliseconds_since_epoch": 1431086652240, "date": "05-08-2015"}
I want to know how to show this result in result.jsp
Today, I found there is resttemplate can use in spring,I want to know are there the tools like restemplate in struts2 that I can use??
My current solution: 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;
}
}
file 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>
Upvotes: 0
Views: 5489
Reputation: 11992
Two things:
1) Create a JavaBean of the object. The names of the fields in the JavaBean must match the JSON's fields (though you can get around this if need be).
public class JsonTest{
private String time;
private Long milliseconds_since_epoch;
private Date date;
//setters and getters
}
2) Use Gson to marshal the JSON Stirng into an object.
public static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
then to use it...
JsonTest object = gson.fromJson(jsonString, JsonTest.class);
to get JSON String from any object....
String json = gson.toJson(someObject);
Upvotes: 0
Reputation: 106
You need to do 2 things:
1.- Once that you get the String from the URL (your result variable) you need to cast into an Object, for example, you can create a Java Bean called Response, this object will look like this:
public class Response{
private String time;
private Long milisenconds;
private Date date;
//setters and getters
}
2.-Now you can cast your string to convert into a Response object using the jackson-mapper library , will look like this:
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.-And also you need to add a new library to your project to support JSON responses, you can find this library : struts2-json-plugin
And in your package/action configuration instead of return a page, you will return a json type:
<?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.- So now you can call this url: http://localhost:8080/yourProyect/GoToGetApi.action and you will see the reponse in json format.
But if you only one to "print" the json string in your jsp, you can do that just adding this in your action class:
private String apiResponse;
//get of apiResponse
public String execute(){
//your current code here
System.out.println(result);
apiResponse=result;
return SUCCESS;
}
And in your jsp you only need to add:
<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>
Upvotes: 1
Reputation: 3669
You could use
1) Java script to parse the JSON string and display the values
or
2) Java Script libraries like AngularJS and JQuery have in built support to do them easily and efficiently with minimal code.
Upvotes: 0