"Could not find acceptable representation" using spring-boot-starter-web
Solution 1
You have no public getters for your UpdateResult, for example :
public static class UploadResult {
private String value;
public UploadResult(final String value)
{
this.value = value;
}
public String getValue() {
return this.value;
}
}
I believe by default auto discovery is on and will try to discover your getters. You can disable it with @JsonAutoDetect(getterVisibility=Visibility.NONE)
, and in your example will result in []
.
Solution 2
I had a similar error using spring/jhipster
RESTful service (via Postman
)
The endpoint was something like:
@RequestMapping(value = "/index-entries/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<IndexEntry> getIndexEntry(@PathVariable Long id) {
I was attempting to call the restful
endpoint via Postman
with header Accept: text/plain
but I needed to use Accept: application/json
Solution 3
I too was facing a similar issue. In my case the request path was accepting mail id as path variable, so the uri looked like /some/api/[email protected]
And based on path, Spring determined the uri is to fetch some file with ".com" extension and was trying to use different media type for response then intended one. After making path variable into request param it worked for me.
Solution 4
If using @FeignClient, add e.g.
produces = "application/json"
to the @RequestMapping annotation
Solution 5
If you are using Lombok, make sure it have annotations like @Data or @Getter @Setter in your Response Model class.
crowmagnumb
Computer programmer and all around enthusiast living in Portland Oregon. I love bikes, camping, dancing, music, playing boardgames. Just bought a compilation today of the first two Big Star albums on one CD. Super psyched!
Updated on July 08, 2022Comments
-
crowmagnumb almost 2 years
I am trying to use
spring-boot-starter-web
to create a rest service serving up JSON representations of Java objects. From what I understand this boot-starter-web jar is supposed to handle the conversion to JSON through Jackson automatically but I am instead getting this error.{ "timestamp": 1423693929568, "status": 406, "error": "Not Acceptable", "exception": "org.springframework.web.HttpMediaTypeNotAcceptableException", "message": "Could not find acceptable representation" }
My Controller is this...
@RestController @RequestMapping(value = "/media") public class MediaController { @RequestMapping(value = "/test", method = RequestMethod.POST) public @ResponseBody UploadResult test(@RequestParam(value="data") final String data) { String value = "hello, test with data [" + data + "]"; return new UploadResult(value); } @RequestMapping(value = "/test2", method = RequestMethod.POST) public int test2() { return 42; } @RequestMapping(value = "/test3", method = RequestMethod.POST) public String test3(@RequestParam(value="data") final String data) { String value = "hello, test with data [" + data + "]"; UploadResult upload = new UploadResult(value); return upload.value; } public static class UploadResult { private String value; public UploadResult(final String value) { this.value = value; } } }
My
pom.xml
has...<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>1.2.1.RELEASE</version> <scope>provided</scope> </dependency>
mvn dependency:tree
shows that spring-boot-starter-web does indeed depend on the jackson2.4 databind and thus should be on the classpath...[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.1.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.2.1.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot:jar:1.2.1.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.1.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.2.1.RELEASE:compile [INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.8:compile [INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.8:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.14:runtime [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.4:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.0:compile [INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.4.4:compile [INFO] | +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile [INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile [INFO] | | \- com.fasterxml:classmate:jar:1.0.0:compile [INFO] | +- org.springframework:spring-web:jar:4.1.4.RELEASE:compile [INFO] | | +- org.springframework:spring-aop:jar:4.1.4.RELEASE:compile [INFO] | | | \- aopalliance:aopalliance:jar:1.0:compile [INFO] | | +- org.springframework:spring-beans:jar:4.1.4.RELEASE:compile [INFO] | | \- org.springframework:spring-context:jar:4.1.4.RELEASE:compile [INFO] | \- org.springframework:spring-webmvc:jar:4.1.4.RELEASE:compile [INFO] | \- org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
... yet calling the
test
service gives the error mentioned above.test2
andtest3
work fine proving that it must just be the attempted conversion to JSON that is failing? Am I missing some configuration problem or annotations? From all the examples I can find, annotating the class for basic Jackson JSON conversion is no longer necessary.Any help greatly appreciated.
-
Asgher over 8 yearswell spring is very "helpful" with HttpMediaTypeNotAcceptableException error message ...
-
Vlasec over 8 yearsI encoutered the same problem, having an empty class (so far) with no fields or getters. I agree the error message is very "helpful", so thanks a lot.
-
crowmagnumb over 7 yearsI show above the correct jackson dependency (not this one) is already there. So this is not a solution to the problem. The solution was already given by @ikumen
-
crowmagnumb over 7 yearsYeah, I think "Could not find acceptable representation" will show its ugly head in a myriad of ways. :(
-
insan-e almost 7 yearsI had a JSON API, but in one route i was returning
produces = MediaType.TEXT_HTML_VALUE
(raw HTML..). But when an exception was thrown (I wanted a JSON error model), Spring probably didn't know how to convert it to string... :/ When I removed theproduces
part it worked.. :D -
insan-e almost 6 yearsI tried to return
List<MyClass>
but saidproduces = MediaType.APPLICATION_XML_VALUE
so Spring doesn't know how to represent the list as XML.. :D obviously... -
Joe over 5 yearsWe will get the same error when this dependency is missing. So please don't down vote this answer as this is correct for some cases.
-
crowmagnumb over 5 yearsWell I can't un-downvote it now as it won't let me. But its still not an answer to my original question. I hope it does help some others though.
-
Cavva79 about 5 yearsThis is not for the above problem, but if you use
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }
and ask forapplication/xml
it is right solution -
silentsudo over 4 yearsWonderfule, i was using lombok, added all arg and no arg anotation and forgotten getter setter completely. Thanks!
-
GuiRitter over 4 yearsThanks! I was trying to do this but it didn't work for the same reason.
-
tufac2 over 2 yearsExactly. I forgot the getters and setters. In fact, the request was incoming with all the fields as null
-
Groucho over 2 yearsI was having a great deal of difficulty getting my REST services to return json without this error on an old Spring application without boot. This did the trick for me, very helpful!
-
Prabhakar over 2 yearsThis opened my eyes. I have used Lombok but forgot to set it up for eclipse. That's why getter and setters were not getting populating for bean even if I have used the @Data annotation. Thanks
-
Himanshu Suthar almost 2 yearsthis worked for me. I use Lombok and forgot to add annotations. Thank you.