我有一个POJO课程
public class Stock{
int id;
String name;
Date date;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何注释或开发框架/ api可以将POJO转换为JSON模式,如下所示
{"id":
{
"type" : "int"
},
"name":{
"type" : "string"
}
"date":{
"type" : "Date"
}
}
Run Code Online (Sandbox Code Playgroud)
并且我可以通过在POJO上指定一些注释或配置来扩展模式以添加诸如"必需"之类的信息:"是",每个字段的描述等,并且可以生成如下的JSON模式.
{"id":
{
"type" : "int",
"Required" : "Yes",
"format" : "id must not be greater than 99999",
"description" : "id of the stock"
},
"name":{
"type" : "string",
"Required" : "Yes",
"format" : "name must not be empty and must be 15-30 characters length",
"description" : "name of the …Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的POJO:
public class LoginPojo {
private String login_request = null;
private String email = null;
private String password = null;
// getters, setters
}
Run Code Online (Sandbox Code Playgroud)
经过一番搜索,我发现了这个: JSONObject jsonObj = new JSONObject( loginPojo );
但是有了这个,我得到了错误:
The constructor JSONObject(LoginPojo) is undefined
Run Code Online (Sandbox Code Playgroud)
我找到了另一个解决方
JSONObject loginJson = new JSONObject();
loginJson.append(loginPojo);
Run Code Online (Sandbox Code Playgroud)
但这种方法不存在.
那么如何将我的POJO转换为JSON?
我正在尝试使用 Junit 和 Mockito 测试我的服务。我能够模拟该服务并从资源类中获取响应。响应正文包含一些 JSON 格式的数据,该数据属于对象类型。我正在尝试使用 Spring Boot 使用 Jackson 库的 ObjectMapper 从对象中获取数据。
EmployeeResource.java
package com.ems.resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ems.exception.InvalidInputDataException;
import com.ems.service.EmployeeService;
import com.ems.service.UserService;
import com.ems.util.EmailUtils;
import com.ms.base.beans.EmailObject;
import com.ms.base.entity.Employee;
import com.ms.base.entity.User;
@RestController
@RequestMapping(path = "/employee")
public class EmployeeResource
{
final Logger logger = LogManager.getLogger(EmployeeResource.class);
final String EMAIL_QUEUE_NAME = "EmailQueue";
@Autowired …Run Code Online (Sandbox Code Playgroud)