0

I have following class

public class Employee {

@JsonPropery("name")
private String name;

@JsonPropery("address")
private Address address;

// getter setters
}

And

public class Address {

@JsonPropery("street")
private String name;

@JsonPropery("location")
private String name;

// getter setters

}

Now while generating the JSON for this model, following error is obtained:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:251)
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183)

Can you Please help out in what should be used in place of @JsonProperty here.

If the referenced class is removed , Proper JSON is obtained. Is there any specific annotation to be used for class reference ?

Sanyam Goel
  • 2,138
  • 22
  • 40
  • I guess the problem is in your call to the rest, in the request headers set Accept header to **application/json** – Bob Zant Jul 04 '17 at 07:44
  • Possible duplicate of [Spring MVC - HttpMediaTypeNotAcceptableException](https://stackoverflow.com/questions/7197268/spring-mvc-httpmediatypenotacceptableexception) – Luciano van der Veekens Jul 04 '17 at 07:44
  • @Luciano van der Veekens 1 this is a different issue. I have already checked the earler one and configuration is correct – Sanyam Goel Jul 04 '17 at 07:47
  • @BobZant that is also there – Sanyam Goel Jul 04 '17 at 07:50
  • Posting the JSON you tried will be much helpful to identify the problem. – cнŝdk Jul 04 '17 at 07:52
  • @ chsdk I do not get a JSON in place server throws an error. – Sanyam Goel Jul 04 '17 at 07:53
  • @chsdk On removing class reference I could obtain a JSON so any specific handling required for class reference ? – Sanyam Goel Jul 04 '17 at 07:54
  • @SanyamGoel Ok, I got it. Don't you have an `Id` in your adress class? – cнŝdk Jul 04 '17 at 07:55
  • @chsdk no I do not have – Sanyam Goel Jul 04 '17 at 08:26
  • @SanyamGoel I thought you can try using [**JsonIdentityInfo** annotation](https://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonIdentityInfo.html) with your `Adress` field, for further details you can check **https://stackoverflow.com/questions/19118059/jackson-serialize-one-attribute-of-a-reference**. – cнŝdk Jul 04 '17 at 08:36

0 Answers0