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!
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.
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.
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"
]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With