springboot jackson returns array instead of proper json object

本小妞迷上赌 提交于 2020-01-03 02:19:04

问题


I have the following controller code.

@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 holds a list of Test objects(with getters/setters and some other attributes) as below

private List<Test> objects;

Test class is defined as below

@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() method is defined as below.

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

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"}

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.

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


回答1:


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



来源:https://stackoverflow.com/questions/37954728/springboot-jackson-returns-array-instead-of-proper-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!