springboot jackson返回数组而不是正确的json对象 [英] springboot jackson returns array instead of proper json object

查看:327
本文介绍了springboot jackson返回数组而不是正确的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器代码。

@RequestMapping(value = "/testService/test", produces = MediaType.APPLICATION_JSON_VALUE)
public HttpEntity<TestBean> test(@RequestParam Map<String,String> testReq)     {
...
List<Test> objList=testRepository.test();
testBean.setObjects(objList);
...
return new ResponseEntity<TestBean>(testBean, HttpStatus.OK);
}

TestBean包含一个Test对象列表(带有getter / setter和其他一些属性) )如下所示

TestBean holds a list of Test objects(with getters/setters and some other attributes) as below

private List<Test> objects;

测试类定义如下

@Entity
@Table(name="TEST")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Test implements Serializable{
private static final long serialVersionUID = -5319848003675140194L;
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="TEST_ID")
Integer testId;
...

test()方法定义如下。

test() method is defined as below.

@Query(value="SELECT ...", nativeQuery=true)
List<Test> test();

我看到的json输出如下所示

The json output I see is as below

{"requestId":"testRequestId","objects":[[1,"Test name"],[2,"Test name2"]],"status":"Success"}

其他事情还可以,我的问题是为什么对象列表会以这种方式输出像这样

Other things being okay, my question is why is the objects list being output in that way instead of like this

{"testId":1,"testName":"Test name"}

对于我正在使用的其他类似对象,我看到了预期的输出(以正确的json格式)。

For another similar objects that I am using I see the output as expected(in proper json format).

请注意,我没有复制实际代码,但键入了更改名称的代码,因此请忽略您在代码中看到的任何语法错误。

Please note that I have not copied the actual code, but typed the code with changed names, so please ignore any syntax errors that you may see in the code.

有人可以告诉我如何在输出中获得正确的json?

Could someone please advise on how can I get a proper json in the output?

推荐答案

我得到了问题。问题是我使用的是为不同对象创建的JPA存储库类。
我为Test对象创建了一个新的JPA存储库类,它现在工作正常,我正确地形成了json输出。
谢谢

I got the issue. The problem was I was using a JPA repository class that was created for a different object. I created a new JPA repository class for Test object and it works fine now, I am getting correctly formed json output. Thanks

这篇关于springboot jackson返回数组而不是正确的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆