gon250
gon250

Reputation: 3545

Java - @ResponseBody String - error 200

I'm trying to get a string response from my controller but I get the below error:

SyntaxError: Unexpected end of JSON input(…) "Error 200"

When I change the response to a boolean or a different type, it's working ok. The problem is when I try to return a string.

js code:

 $.ajax({
   method: "POST",
   url: "./signup",
   data: _data,
   dataType: "json",
   contentType : "application/json;charset=UTF-8",
   success : function(data) {
       console.log(data)
   },
   error : function(qXHR, textStatus, errorThrown){
       console.log(errorThrown, "Error " + qXHR.status);
   }
  });

controller code:

@RequestMapping(value = "/signup", method = RequestMethod.POST, produces = {"text/plain", "application/*"})
    public @ResponseBody String signup(@RequestBody UserSignup details) {
       //...
        return message;
    }

any idea how can I solve this problem? I have tried a few things but nothing work. I think the response format is wrong as what the code expects.

Edit

I have changed my code(removed produces) but I still getting the same error:

SyntaxError: Unexpected end of JSON input(…) "Error 200"

@RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody String signup(@RequestBody UserSignup details) {
        message = "ok";
        }
        return message;
    }

Upvotes: 2

Views: 1384

Answers (3)

gon250
gon250

Reputation: 3545

As I don't have problem when the response is different stuff as a String I have solved the problem creating my own object. So below is the code:

public class Response<T> {
    private T message;
    private Exception ex;

    public Exception getEx() {
        return ex;
    }

    public void setEx(Exception ex) {
        this.ex = ex;
    }

    public T getMessage() {
        return message;
    }

    public void setMessage(T message) {
        this.message = message;
    }
}


@Controller
public class MyControllerController {
    private Response<String> _response;
    private String message;

    public MyController() { _response = new Response<>(); }

 @RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody Response<String> signup(@RequestBody UserSignup details) {
        try{
            message = "";
             // code...
            _response.setMessage(message);
            return _response;
        }catch (Exception ex){
            _response.setEx(ex);
            return _response;
        }
    }
}

response example in the browser:

Object {message: "", ex: null}

Upvotes: 0

Kamil Banaszczyk
Kamil Banaszczyk

Reputation: 1153

Try to wrap your response in ResponseEntity class

@RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody ResponseEntity<String> signup(@RequestBody UserSignup details) {
        message = "ok";
        return new ResponseEntity<>(message, HttpStatus.OK);
    }

Also double check data that you are sending to server, maybe this is the problem, can you show us _data value?

Upvotes: 0

cralfaro
cralfaro

Reputation: 5948

Your method is wrong. You are saying to produce produces = {"text/plain", "application/*"} But you are also adding the @ResponseBody which will generate JSON format response.

I would suggest you remove the attribute produces. And verify the string you are returning is well formed

Upvotes: 5

Related Questions