Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

What this request provide (problem with method argument)

 
Ranch Hand
Posts: 39
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am trying to write a method that will respond to this http request. Unfortunately, I do not know what the argument of such a method should be.



swagger:



At first I thought it would be a Multipartfile and then I would just process it (see dummy approach below), but then I saw a "string" in swagger and it confused me a bit. I have no clue anymore what the format of the input data is.
(I know- I should use nio, no io)



 
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused. You appear to be attempting to POST a CSV file named "data.csv". You post a file labelled "swagger", which is a YAML file (not CSV). Probably there's a MIME (Content-Type) more appropriate to use than "text/plain"  for CSV. Like, say, "text/csv". But that's secondary.

Youd server-side code appears to expect MIME-multipart input, but your curl command is set up to simply blast raw data to the server without form variable naming, MIME or multi-partitioning. And that's OK, as long as the server doesn't try and treat it like is is any of those creatures. You want to configure to receive an InputStream, String, or similar character sequence without the extra stuff that an HTTP FORM submit would add.
 
Jan Kaczmarek
Ranch Hand
Posts: 39
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This swagger is just representing my API endpoint documentation (this is the PUT part of it).
CSV example (the one I made for tests)

5,Yesterday,All,root
3,My,Troubles,2
4,Seemed,So,5
0,Far,Away,2
7,Now,it,2
6,Looks,as,3
8,though,they're,5
1,here,to,8
9,stay,oh,4



I cannot edit the request. I have to conform to it. The whole problem is with this part:
 
Sheriff
Posts: 22153
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multipart is a bad choice for CSVs. It's meant to send multiple values like a regular form post, but with files as well. MultipartFile is used for one element of a multipart request. You're not sending a multipart request so that's not going to work.

Your specifation says that the content type is "text/plain", and the type of the body parameter is "string". That means that you should just use String as body type, and parse the content yourself. A better option would be to use "text/csv", but I'm not sure how this can be easily mapped to objects.
 
Tim Holloway
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I agree with Rob - I'm just out of practice so I wasn't going to make foolish suggestions.

As far as MIME type text/csv, I don't think it's going to make any difference functionally, but it's good practice.
 
Jan Kaczmarek
Ranch Hand
Posts: 39
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the method accepting such a request will look like this :



because this request actually accepts a CSV file, but passes all its contents to me on the server side in the form of a String, am I right?
After this PUT I will have argument, that looks like:

 
Tim Holloway
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good to me.
 
Rob Spoor
Sheriff
Posts: 22153
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a library like opencsv or Apache Commons CSV to parse the contents. I think you can even use Jackson with jackson-dataformat-csv to parse it directly to objects.

Whatever you do, don't parse the contents yourself. CSV may look simple, but you may get unexpected results if the CSV becomes a bit more advanced (like including quotes around string values).
 
Jan Kaczmarek
Ranch Hand
Posts: 39
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:You can use a library like opencsv or Apache Commons CSV to parse the contents. I think you can even use Jackson with jackson-dataformat-csv to parse it directly to objects.

Whatever you do, don't parse the contents yourself. CSV may look simple, but you may get unexpected results if the CSV becomes a bit more advanced (like including quotes around string values).



The problem is that these libraries work on CSV files - I get it in the form of one String on the server side, so I probably have to choose the form
 
Tim Holloway
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, Apache OpenCSV for one allows parsing CSV strings. I didn't check the others, but it's to be expected.

Plus, pretty much any textfile parser can parse strings by using a String Reader.
 
Rob Spoor
Sheriff
Posts: 22153
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Plus, pretty much any textfile parser can parse strings by using a String Reader.


That statement is so important I'm quoting it. Every String can be turned into a Reader by using StringReader. Every byte[] can be turned into an InputStream by using ByteArrayInputStream. That allows you to use String and byte[] with code that can handle any type of text or binary input.

Besides that, Jackson's ObjectMapper has readValue methods that take a String. Combined with the data format I linked to you can have Jackson convert the CSV string into POJOs for you.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic