• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring can't handle array input

 
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems like a very simple problem, but I just cant make it work



this will always result in HTTP 406 -> HttpMediaTypeNotAcceptableException : Could not find acceptable representation

I know we can use wrapper class to bag the array, but surely there's a way without needing to resort to such trick?

Help anybody?

Thanks
 
Marshal
Posts: 4499
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The annotation says consumes = "application/json", indicating that a JSON payload is expected.

  - Is the client side actually sending JSON?
  - Does the request include a Content-Type: application/json header to indicate that the payload is JSON?
 
Mark Sando
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep, included Content-type header of application/json and the payload is in valid json format.
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this stackoverflow help:
https://stackoverflow.com/questions/55393032/spring-rest-service-throw-406-not-acceptable-org-springframework-web-httpmedi
?
 
Mark Sando
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
unfortunately, it doesnt. so, I tried if the input is just one Request, it'd work. The problem only happens when the endpoint accepts array
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Sando wrote:I tried if the input is just one Request, it'd work. The problem only happens when the endpoint accepts array


Are you saying this is what works for you?

If so, the problem is not that the endpoint is accepting a List (not an array), it's that you're returning a List.

See if anything in this article helps you: https://www.baeldung.com/spring-resttemplate-json-list
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Do you input an array of Json objects like this in Postman:
[
  {
      "request" : "request 1"

   },

  {
      "request": "request 2"
   }
]
?
 
Mark Sando
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Himai -> yes, that's exactly how my payload looks like

@Junilu -> for the endpoint that works, if I modify the return as list, it will still work. only fails once I change the @RequestBody method param into array
 
Mark Sando
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure if it's of any relevance, I use postman to hit the endpoints
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Mark,
How about this kind of parameter :  @RequestBody List<User> users  ?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Sando wrote:Not sure if it's of any relevance, I use postman to hit the endpoints


Please give us a curl command you get from postman. Look for </> on the right hand side of postman and please copy/paste what you see there.
 
reply
    Bookmark Topic Watch Topic
  • New Topic