If you create a Web Project for testing and delete it when you're done, you don't have to be Maven, but I'll make it available for a while.
If you can't find the menu in New> Other> Maven> Maven Project, the plugin is missing and you should install it. In Help> Install New Software, select and install as follows.
GropuId: qiita.keniooi // In the sense for this page, ArtifactId: sampleWeb // As a project name Version: Default Package : sample.web
Press Finish and you're done.
Write the required library in pom.xml so that you can use it. Here, add the JSON library.
JSON In Java Click 20180130 on this site and copy from the following part.
Open the pom.xml in your sampleWeb project and add the copied text inside the dependencies tag.
pom.xml
...
<dependencies>
<dependency>
<groupId>net.wasdev.maven.tools.targets</groupId>
<artifactId>liberty-target</artifactId>
<version>RELEASE</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
...
With this setting, you can see that it has been added to Maven Dependencies.
I am planning to create a Servlet that receives JSON by POST and processes it, but here, I will create a Servlet that processes String by JSONArray and JSONObject in the Servlet. Since it is written in doPost (), doGet () for testing is just calling doPost ().
TestServlet.java
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json;charset=UTF-8");
String json = "[{\"name\":\"Satoh Taro\",\"id\":\"123\",\"mail\":\"[email protected]\"},"
+ "{\"name\":\"Suzuki Hana\",\"id\":\"124\",\"mail\":\"[email protected]\"}]";
JSONArray ja = new JSONArray(json);
ArrayList<Person> list = new ArrayList<Person>();
for(Object o : ja) {
JSONObject jo =(JSONObject) o;
Person p = new Person();
p.setName(jo.getString("name"));
p.setId(jo.getString("id"));
p.setMail(jo.getString("mail"));
list.add(p);
System.out.println(p.getId() + "," + p.getName() + "," + p.getMail());
}
response.getWriter().println(json);
}
}
class Person {
String name;
String id;
String mail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
When you access this Servlet with Firefox, it looks like this:
Liberty's log shows
stdout
123,Satoh Taro,[email protected]
124,Suzuki Hana,[email protected]
Is output.
From installation to project creation, a convenient development environment called Eclipse + Maven [Java] How to handle JSON data with standard API, Jackson, JSON in Java
Recommended Posts