1

I need to have a method that does not regard/parse the content of request message, just ... pass it along as input parameter to the @PostMapping method. Is it possible? Because defining parameters like:

@RequestBody byte[] data

or

@RequestBody String text

tell the framework that it suppose to get some xml/json. and I want it to receive plain text + utf-8 encoding.

Some code to clarify:

@RestController
@RequestMapping(path="/abc", method = RequestMethod.POST)
public class NlpController {
    @PostMapping(path="/def", consumes="text/plain; charset: UTF-8", produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> processText(@RequestBody String text)
    {
        ... 
        return ResponseEntity.ok().body(object);
    }
}

Trying also:

@RestController
@RequestMapping(path="/abc", method = RequestMethod.POST)
public class NlpController {
    @PostMapping(path="/nlp", consumes=MediaType.APPLICATION_JSON_UTF8_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> process(HttpServletRequest request)
    {
        ....
        return ResponseEntity.ok().body(article);
    }
}

But I get 406 response...

using curl:

curl -v -s -X POST -H "Content-Type:" -H "Content-Type: application/json; charset: utf-8" --data-binary @article.txt localhost:8080/abc/def/
rubmz
  • 1,947
  • 5
  • 27
  • 49
  • You can get the underlying HttpRequest – Marged Sep 17 '19 at 17:47
  • See updated question with code - I cannot pass through the framework's request resolution process - since the parameters expected just aren't there. I want to pass the whole body as a parameter, not a variable. Not sure it CAN be done... Hence the question. – rubmz Sep 17 '19 at 18:37

3 Answers3

1

I think you should inject HttpServletRequest as controller method attribute, then you will have acces to request payload.

@PostMapping(path="/something")
public ResponseEntity<Object> processText(HttpServletRequest request) {
// do something with request
}

More info.

Kamil W
  • 2,230
  • 2
  • 21
  • 43
  • Interesting yet I still get: 406 response. updating my question with your suggestion. – rubmz Sep 17 '19 at 19:13
  • @rubmz I supposse you set wrong consumed or produced content (or you missing correct header) play around this stuff. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406 – Kamil W Sep 17 '19 at 19:17
  • Nope. This does not help either ... And I played with all flags possible on both server and client ends. – rubmz Sep 18 '19 at 21:04
1

406 Not Acceptable

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

If i understand your question correctly you need to load text file directly as input param in spring boot rest call.

You need to modify your code and curl request , please use fllowing code as referance .

   @RequestMapping(value = "/abc", method = RequestMethod.POST)
      public String ResponseEntity<Object> processText(@RequestParam("file") 
    MultipartFile file) {        
    System.out.println("---------loading file----------");
    /// Calculation and your logic 

    return ResponseEntity.ok().body(article);
  }

Curl request :

       curl -X POST localhost:8080/abc -F "file=@article.txt"

One more issue i can see in your curl request your mapping is abc and you are calling localhost:8080/abc/def/

If using data in memory following code will work for you

  @PostMapping(value = "/abc", consumes = "application/json", produces = 
   "application/json")
    ResponseEntity<Object> processText( @RequestBody String input)
        throws  JSONException {
    //
    return ResponseEntity.ok().body(article);
    }
vaquar khan
  • 10,864
  • 5
  • 72
  • 96
  • Cool multipart example. Yet I won't be using files as input. currently I am just using files to test the server, but I will be using another server with data in memory. Still didn't find the way to bypass that 406... maybe it is one of the 'accept-*' headers. – rubmz Sep 17 '19 at 19:51
0

Short answer: This is not a job for a full blown framework like spring boot. Better use something like spark that can do this with one liner and without any configurations. At least this is the best answer for my humble causes.

Long answer: I could not make spring boot to receive clean body text from a client, not even after many (failed) attempts to tweak the headers / media / consume flag / ... Guess this just (might) not be possible.

rubmz
  • 1,947
  • 5
  • 27
  • 49