kajarigd
kajarigd

Reputation: 1309

Need to return Boolean @ResponseBody. Now getting HTTP 406 error

I am trying to return a boolean as a HTTP Response in a web application (REST, Spring, JPA Hibernate). Here's the code:

@ResponseBody
@RequestMapping(value="/movieTheater", method=RequestMethod.GET)
public boolean getCustomerInput(Map<String, Double> input) {
    return transactionService.addTransaction(input);
}

Now, I guess this is not allowing me to return a boolean, but expecting something else. When I am trying to access in the browser the following:

http://localhost:8081/SpringMVCMerchant/movieTheater.htm

I am getting the following error:

HTTP Status 406 -

type Status report

message

description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

Can you please tell me a way to send boolean as a response? If not, what else can I do? Thanks in advance!

Upvotes: 6

Views: 3972

Answers (3)

Liuxiaokey
Liuxiaokey

Reputation: 1

Update your version of spring to 3.1.3.release or higer.Then the @Responsebody will convert the boolean to JSON correctly.

Upvotes: 0

Kunalan S
Kunalan S

Reputation: 154

Seems issue in produce JSON data, add the property produces like this

@RequestMapping(value="/movieTheater", method=RequestMethod.GET,
            produces={MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody ResponseText getCustomerInput(Map<String, Double> input) {
    //TODO:
}   

Upvotes: 0

Kunalan S
Kunalan S

Reputation: 154

step 1: create an Enum

public enum ResponseStatus {
    SUCCESS("true"),
    FAILED("false");
    private final String status;

    private ResponseStatus(String status) {
       this.status = status;
    }

    public String getStatus() {
       return status;
    }
}

step 2: create a class for returning the response details

public class ResponseText {
    private String message;

    public String getMessage() {
        return message;
    }

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

step 3: modify the code as following

@ResponseBody
@RequestMapping(value="/movieTheater", method=RequestMethod.GET)
public ResponseText getCustomerInput(Map<String, Double> input) {
    ResponseText result = new ResponseText();
    if(transactionService.addTransaction(input))
        result.setMessage(ResponseStatus.SUCCESS.getStatus());
    else
        result.setMessage(ResponseStatus.FAILED.getStatus());
    return result;
}

now you can get output JSON like this

{[
    message:"true"
]}

Upvotes: 1

Related Questions