作者:乔父系_377 | 来源:互联网 | 2023-08-13 13:23
原标题:JavaWeb实体类转为json对象
实体类转为json对象
- 1.创建个实体类
- 2.使用类库 fastjson
- 3.测试一下
- 4.Controller代码
1.创建个实体类
实体类User代码:
package com.neuedu.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String address;
private Date date;
}
我没有自己写构造www.yii666.com函数和Setter(),Getter()以及toString()方法,而是使用了lombok自动生成的。
2.使用类库 fastjson
添加依赖:
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.73version>
dependency>
3.测试一下
JsonTest 代码:
package com.neuedu.entity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JsonTest {
@Test
public void test(){
List userList = new ArrayList();
userList.add(new User(1,"牛哄哄的柯南1","某某省某某市1",new Date()));
userList.add(new User(2,"牛哄哄的柯南2","某某省某某市2",new Date()));
userList.add(new User(3,"牛哄哄的柯南3","某某省某某市3",new Date()));
userList.add(new User(4,"牛哄哄的柯南4","某某省某某市4",new Date()));
userList.add(new User(5,"牛哄哄的柯南5","某某省某某市5",new Date()));
userList.add(new User(6,"牛哄哄的柯南6","某某省某某市6",new Date()));
String json = JSON.toJSONString(userList, SerializerFeature.WriteDateUseDateFormat );
System.out.println(json);
}
}
运行结果:
[{"address":"某某省某某市1","date":"2020-12-17 21:09:21","id":1,"name":"牛哄哄的柯南1"},
{"address":"某某省某某市2","date":"2020-12-17 21:09:21","id":2,"name":"牛哄哄的柯南2"},
{"address":"某某省某某市3","date":"2020-12-17 21:09:21","id":3,"name":"牛哄哄的柯南3"},
{"address":"某某省某某市4","date":"2020-12-17 21:09:21","id":4,"name":"牛哄哄的柯南4"},
{"address":"某某省某某市5","date":"2020-12-17 21:09:21","id":5,"name":"牛哄哄的柯南5"},
{"address":"某某省某某市6","date":"2020-12-17 21:09:21","id":6,"name":"牛哄哄的柯南6"}]
Process finished with exit code 0
4.Controller代码
UserController代码:
package com.neuedu.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.neuedu.entity.User;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@WebServlet(name="UserController" ,urlPatterns = "/user")
public class UserController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List userList = new ArrayList();
userList.add(new User(1,"牛哄哄的柯南1","某某省某某市1",new Date()));
userList.add(new User(2,"牛哄哄的柯南2","某某省某某市2",new Date()));
userList.add(new User(3,"牛哄哄的柯南3","某某省某某市3",new Date()));
userList.add(new User(4,"牛哄哄的柯南4","某某省某某市4",new Date()));
userList.add(new User(5,"牛哄哄的柯南5","某某省某某市5",new Date()));
userList.add(new User(6,"牛哄哄的柯南6","某某省某某市6",new Date()));
String json = JSON.toJSONString(userList, SerializerFeature.WriteDateUseDateFormat );
resp.setC文章来源地址36094.htmlontentType("application/json");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.write(json);
out.flush();
out.close();
}
@Override
protected void doPost(HttpServle文章来源站点https://www.yii666.com/tRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
启动服务访问http://127.0.0.1:8080/web/user
访问结果:
OK,到此就完成实体类转为json对象的需求了。
看完如果对你有帮助,感谢点赞支持!
如果你是电脑端,看到右下角的 “一键三连” 了吗,没错点它[哈哈]
加油!
共同努力!
Keafmdwww.yii666.com
来源于:JavaWeb实体类转为json对象