0

I do not understand why this happens: when I declare explicitly the filename of the input in my annotated java class named "process", everything works perfect:

@GET
public static void process() throws IOException {
    File file = new File("a.pdf");
    FileUtils.writeStringToFile(new File("a.pdf" + ".exported"), menu.parseToString(file));
}

However, when I try to pass the file name as an argument, and configure also eclipse through run configurations to give the appropriate argument (path to the "a.pdf"):

@GET
public static void process(String[] args) throws IOException {
    File file = new File(args[0]);
    FileUtils.writeStringToFile(new File(args[0] + ".exported"), menu.parseToString(file));
}

when I call the service it fails with an error:

Oct 09, 2014 9:44:55 AM org.apache.cxf.jaxrs.utils.JAXRSUtils readFromMessageBody
WARNING: No message body reader has been found for request class String[], ContentType :    
application/octet-stream.

I am new to jax rs. Do I miss any annotation? Thank you very much...

6
  • 1) That public static void process("a.pdf") is not valid Java. Please include the real code you use. 2) "pass the file name as an argument" How are you doin this using a HTTP request?
    – user1907906
    Oct 9, 2014 at 15:19
  • When the service is published I get a link. When I click on this link a.pdf is parsed. So when I explicitly define the file name and click the link on the published service it works. However, when I point out to the file location (a.pdf) and click on the published web service link I get the error mentioned. That's my real code btw. and 1) works/compiles great on my side...
    – pebox11
    Oct 9, 2014 at 15:24
  • What you write makes no sense. "When the service is published I get a link." What link? Where? I don't see any HTML that links anywhere. "when I point out to the file location". How do you do that? Summary: Please show us how you call this JAX-RS resource.
    – user1907906
    Oct 9, 2014 at 15:41
  • "link" = identifies the URI path template to which the resource responds. This part of code I posted is a resource which I have annotated accordingly and published it as a jax rs (see for example docs.oracle.com/cd/E19798-01/821-1841/ginpw/index.html to understand what do I mean by link). I am sorry if this upset you my friend. Probably yes I shouldn't have used "link" but "resource URI" instead.
    – pebox11
    Oct 9, 2014 at 16:01
  • I know the @Path annotation. Please tell us the details of the HTTP request you make. The infromation you provided so far is not enought to understand your problem.
    – user1907906
    Oct 9, 2014 at 16:03

1 Answer 1

2

Invalid Java syntax

This

public static void process("a.pdf") throws IOException {
  // ...        
}

is not valid Java syntax. Every IDE and javacwill complain about it. Eclipse says:

Syntax error on token ""a.pdf"", delete this token

application/octet-stream as body

It looks like you try to make a GET request with Content-Type: application/octet-stream (the file you somehow 'clicked'). This has two problems:

  1. A GET request normally as no body, it just has headers.
  2. A body of bytes encoded as application/octet-stream can not be mapped to a String[] because JAX-RS has no way to know how to intepret the bytes.

What you probably want

It looks like you want to make a GET request with a file name as a request parameter (that is not the sames as a file in the body of the request). You could do this:

GET http://example.com/service?filename=foo.pdf

Then a JAX-RS like the following could be used to service this request:

@GET
public Response service(@QueryParam("filename") String filename) {
  // use filename to open a File and do something with it
}

Note the use of @QueryParam which allows service to extract filename=foo.pdf from the request URL.

3
  • Yes you are correct about that ("a.pdf" is not needed inside process() - I deleted it from my post), however the rest stays the same and still the problem remains, why when I explicitly (hard code lets say) declare the filename jax rs is not complaining and when I try to do it by passing filename as an argument it fails? I will evaluate your proposed solution and I will get back to you. Thank you for your patience.
    – pebox11
    Oct 9, 2014 at 16:25
  • You are not passing the filename as a String argument. Not as a path argument, not as a query parameter, and not as the request body, too. You are passing some bytes as application/octet-stream. Where do the come from? Are they the file name or the file content? Unless you provide more details about how exactly you make the HTTP GET request, I can only guess.
    – user1907906
    Oct 9, 2014 at 16:34
  • Thank you for your patience. The file is on a folder on the server that hosts the jax-rs service. So when I publish the service explicitly referring to the filename, the file gets discovered when I hit the URI of the published service, however when I give as an argument the path to the filename the file resides, for some reason I get this error I mentioned. I want to know the reason that causes the error.
    – pebox11
    Oct 9, 2014 at 17:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.