【发布时间】:2018-09-04 02:57:30
【问题描述】:
首先,非常感谢您阅读这个问题。
我有一个 JPA 项目,一切正常,我通过控制器获得的 json 是这种形式:
{"id": 1, "name": "Canada"},{"id": 2, "name": "USA"}
一切都很好,但我想获得一个符合 Jsend 标准的 json,它是这样的:
{
status : "success",
data : {
"country" : [
{"id": 1, "name": "Canada"},
{"id": 2, "name": "USA"}
]
}
}
{
"status" : "fail",
"data" : { "title" : "A title is required" }
}
{
"status" : "error",
"message" : "Unable to communicate with database"
}
如您所见,我想要一个状态为成功、失败或错误: 但我不知道该怎么做。这是我的 DTO、DAO 和控制器
@Entity
public class Country implements Serializable {
private static final long serialVersionUID = -7256468460105939L;
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
//Constructor, get and set
道
@Repository
@Transactional
public class CountryRepository {
@PersistenceContext
EntityManager entityManager;
public CountryDTO findById(int id) {
return entityManager.find(CountryDTO.class, id);
}
}
控制器
@RestController
public class CountryController {
@Autowired
CountryDTO repository;
@RequestMapping(value="api/country/{id}", method=RequestMethod.GET)
public @ResponseBody CountryDTO getByID(@PathVariable("id") int id){
return repository.findById(id);
}
}
再次感谢您的宝贵时间。
【问题讨论】: