Let's look back on the handling of JSON using Jackson in Java. In the case of my project, Java is almost a Web system, and the cooperation between Servlet and JavaScript is important. First of all, I tried to return JSON from the server this time. Except for the implementation in Servlet, most of the references are diverted.
First of all, in order to convert to JSON, it is necessary to create a Java class to store the object for it. The properties of id (number type), name (string type), and datas (array) are prepared as follows. The key is the annotation by @JsonProperty. Without it, an exception will be thrown.
JsonBean.java
package servletTest;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class JsonBean {
@JsonProperty("id")
private int id;
@JsonProperty("name")
private String name;
@JsonProperty("datas")
private List<String> datas;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDatas(List<String> datas) {
this.datas = datas;
}
}
Create an object using the Java class created in the previous section, convert it to a Json string with the writeValueAsString method of the ObjectMapper class, and return it to the client.
SetvletTest.java
package servletTest;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
//Reference https://itsakura.com/java-jackson
@WebServlet("/helloworld")
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Set a value in a Java object
JsonBean jsonBean = new JsonBean();
jsonBean.setId(1);
jsonBean.setName("kimisyo");
List<String> datas = new ArrayList<>();
datas.add("Programmer");
datas.add("Data Scientist");
jsonBean.setDatas(datas);
ObjectMapper mapper = new ObjectMapper();
try {
//Convert from Java object to JSON
String testJson = mapper.writeValueAsString(jsonBean);
//JSON output
response.getWriter().write(testJson);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
When the above is executed in Eclipse, the following character string is displayed in the browser.
{"id":1,"name":"kimisyo","datas":["Programmer","Data Scientist"]}
Originally, it would be better to set Content-Type as well, but I think I will have to do it from the next time onwards, so I postponed it. From the next time onward (if any), we plan to proceed with the following subjects. -~~ Get the JSON received on the client side with JavaScript ~~ --Get JSON sent from client on server side --~~ Escape of JSON string, ~~ XSS countermeasures --Embed JSON in HTML and use it from JavaScript (Java version)
-Convert JSON and Java objects with Java Jackson
Recommended Posts