Springframewok Httpmediatypenotsupportedexception: Content type 'text/Plain;charset=UTF-8' not supported

Asked

Viewed 1,197 times

0

I’ve been trying to find solutions to this mistake and I can’t find.

Error:

2018-07-24 16:44:50.541 WARN 25695 --- [nio-8080-exec-2] .w.s.Defaulthandlerexceptionresolver : Resolved Exception caused by Handler Execution: org.springframework.web.Httpmediatypenotsupportedexception: Content type 'text/Plain;charset=UTF-8' not supported

Algorithm in tscript that is called after a button click:

uploadKeySize(e) {
    let keySize = parseInt(e.target.value);
    console.log(keySize);
    this.fileService.sendInfoToGenKey(keySize).toPromise()
        .then(
        data => {
          console.log(data);
        });
  }

file service method that is called and that communicates with the back:

sendInfoToGenKey(sizeKey: number) {
        return this.http.put('http://localhost:8080/operation/request-RSA-keys', sizeKey);
    }

Code:

@RequestMapping(value = "/request-RSA-keys", method = RequestMethod.PUT)
public static KeyPair generateRSAKeys(@RequestBody int sizeKey) throws NoSuchAlgorithmException {
    System.out.println(sizeKey);
    return KeyGenerator.generateKey("RSA", sizeKey);
}
  • Can you put the request being sent by the client as well? This can help identify the problem.

  • Only one detail that is not what you are asking, but that has to do with your code is that the NoSuchAlgorithmException will never be launched by the method. Therefore, I recommend involving the call of generateKey in a block try and in the catch of NoSuchAlgorithmException launch a AssertionError.

  • Wait a minute, what a class KeyGenerator is that it? It certainly isn’t javax.crypto.KeyGenerator.

  • Can you catch a stacktrace of your mistake? I think you will need to post more about your code so that the source of the problem and the possible solution can be pointed out.

  • The Keygenerator class is one I created. In it is just an "interface" that is called when I call the back method. In it I use the Keypairgenerator

  • 1

    By default, Spring has some pre-enabled Httpmessageconverter(s) that convert a request body to Java objects. If I’m not mistaken, the conversion of a POST text/Plain to int is not enabled. You have a few options: 1 - Work with application/json. 2 - Receive a String sizeKey and convert to int. 3 - Register an Httpmessageconverter to perform the conversion of a POST Plain/text to int. Take a look here and here

  • @Tommelo said it all, you have to simplify your answer

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.