Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 532
kajarigd Avatar asked Aug 06 '13 21:08

kajarigd


People also ask

Why am I getting a 406 error?

The 406 Not Acceptable status code is an error message that means your website or web application does not support the client's request with a particular protocol. Basically, this includes everything from an unsupported protocol to a bad user agent (think Internet Explorer) and more.

How to test 406 error?

The primary way to address and fix a 406 error is by checking the source code for issues in the Accept-, Request-, and Response- headers. The easiest way to review Accept- and Response- headers is to open a webpage in your browser, right-click, and select Inspect.


1 Answers

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"
]}
like image 89
Kunalan S Avatar answered Oct 04 '22 21:10

Kunalan S