I have made a hello world rest service using spring boot. I am able to call the service via standalone java program. I am also able to call it via Advanced Rest Client add-on for Chrome.
But when I try to hit it via a standalone HTML page using jQuery AJAX I am getting error
WARN 3748 --- [nio-9000-exec-2] o.s.web.servlet.PageNotFound : Request method 'GET' not supported
Any Help is appreciated.
PS: I am assuming that as I am able to call the WebService using other modes so the controller is fine. But something is wrong the way I am calling it from the HTML.
HTML Page Below:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Client</title>
<script type="text/javascript" src="jquery-2.2.3.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#Submit").click(function() {
var input = {
"name": $("#name").val(),
"language": $("#language").val()
};
var inputStr = JSON.stringify(input);
alert(inputStr);
$.ajax({
url: "http://localhost:9000/rest/greetMeObj/",
method: "POST",
data: inputStr,
dataType: "jsonp",
success: function(output) { // callback method for further manipulations
var str = JSON.stringify(output);
$("#output").text(data);
},
error: function(data) { // if error occured
$("#error").text(data);
}
});
});
});
</script>
</head>
<body>
<div id="input">
<label><b>Name:</b>
</label>
<input type="text" name="name" id="name" alt="Enter you name" title="Enter your name" />
<br />
<br />
<label><b>Language:</b>
</label>
<select name="language" id="language" title="Select your language">
<option value="en" label="English" selected="selected">English</option>
<option value="fr" label="French">French</option>
<option value="nl" label="Dutch">Dutch</option>
</select>
<br />
<br />
<button title="Submit" type="button" name="Submit" id="Submit" value="Submit" formaction="POST">Submit</button>
</div>
<div id="output"></div>
<div id="error" style="color: red;"></div>
</body>
</html>
Spring Controller Below
@Controller
@RequestMapping("/rest/*")
public class GreetingController {
private static final String TEMPLATE_EN = "Hello, %s!";
private static final String TEMPLATE_FR = "Bonjour, %s!";
private static final String TEMPLATE_NL = "Hallo, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping(value="/rest/greetMe", method= RequestMethod.GET)
public @ResponseBody Greeting sayHello(
@RequestParam(value = "name", required = false, defaultValue = "Stranger") String name,
@RequestParam(value = "language", required = false, defaultValue = "en") String language) {
return new Greeting(counter.incrementAndGet(), String.format(getTemplate(language), name));
}
@RequestMapping(value="/rest/greetMeObj", method= RequestMethod.POST)
public @ResponseBody Greeting sayHello(
@RequestBody(required = true) Input input) {
return new Greeting(counter.incrementAndGet(),
String.format(getTemplate(input.getLanguage()), input.getName()));
}
private String getTemplate(String language) {
String template;
switch (language) {
case "nl":
case "NL":
template = TEMPLATE_NL;
break;
case "fr":
case "FR":
template = TEMPLATE_FR;
break;
case "en":
case "EN":
default:
template = TEMPLATE_EN;
break;
}
return template;
}
}
Check your controller, if you have mapped any of these calls to default mapping:
It is mapped to
path="/"
. Please change it toI guess the solution is described here: Why SpringMVC Request method 'GET' not supported?
Both values in the RequestMapping has to be the same. So for each value you have to define one for GET and one for POST.