Spring Boot rest API encoding error

502
February 24, 2017, at 04:57 AM

I create a small service which take a string from UI textarea, and pass the string to a spring boot rest service, the string include a piece of java code and will be analyzed by spring logic and push the result back into UI again, here I have the string problem

In UI project, the code is:

var sendCode = function(){
      var code = $(".textarea").val();
      console.log(code);
      var url = "http://localhost:8080/simpleoj";
      var success = function(data){
        $("#finalResult").html(data);
      }
      $.ajax({
        type: "POST",
        url: url,
        data: code,
        success: success
      });
    }

where the url is the destination the post should go

In the back end, I have the code as follows:

import org.springframework.web.bind.annotation.*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@RestController
public class WebController{
    @CrossOrigin(origins = "*")
    @RequestMapping(value = "/simpleoj",method = RequestMethod.POST,consumes = {})
    public String greetings(@RequestBody String bodyContent) throws Exception{
        String source =  bodyContent;
        System.out.println(source);
        //I have remove the logic, because it is not important
        }
    }
}

when I deploy all of these, I found the logic can not execute because the bodyContent shows as below:

public+class+Solution%7Bpublic+int+add%28%29%7Breturn+1+1%3B%7D%7D=

which is not what I want!

I want it to be as follows:

public class Solution{public int add{return 1+1;}}

when I test in POSTman, it is working fine. Could someone please help me?

Answer 1

That is because the param is econded as URL. You can use in order to decode

String result = java.net.URLDecoder.decode(bodyContent, "UTF-8");
READ ALSO
Are my channel separation calculations correct?

Are my channel separation calculations correct?

I have an HSV image and I am interested in a certain area of the imageI have gotten image selection to work and determine the area I am interested in but I don't think the way I separate my channels is correct

265
How do you detect two fingers to draw two separate circles?

How do you detect two fingers to draw two separate circles?

I'm trying to create the following code to detect two fingers and have it track the movement with representations of different colored circlesHowever, I'm unsure how to have more than one circle working

354
App Engine Java Flex Environment and MySQL Second Generation are compatibles?

App Engine Java Flex Environment and MySQL Second Generation are compatibles?

Anybody knows if the App Engine Java Flex Environment and MySQL Second Generation are compatibles?

403
Arduino IDE crashes in Gentoo

Arduino IDE crashes in Gentoo

A few days before, I installed arduino-ide in Gentoo from this overlay: https://githubcom/maurerpe/arduino-overlay

467