1

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?

3
  • We need more info about your Test and TestBean classes. Do any of these classes contain Jackson annotations? The array serialization happens when the BeanAsArraySerializer is used, which might be activated if you have certain @JsonFormat annotations. Commented Jun 22, 2016 at 6:45
  • Nonetheless, it's very weird that your ID is also in that array, since you added a @JsonIgnore annotation on the testId field (unless it's a different number in the response). Commented Jun 22, 2016 at 7:09
  • There are no extra annotations on the Test class other than what I have already mentioned. And below is the only annotation on TestBean. @JsonPropertyOrder({ "requestId", "objects" }) public class TestBean extends ResourceSupport { Commented Jun 22, 2016 at 13:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.