我使用弹簧靴进行了hello world rest服务。 我可以通过独立的java程序调用该服务。我也可以通过Chrome的Advanced Rest Client插件来调用它。
但是当我尝试使用jQuery AJAX通过独立的HTML页面点击它时,我收到错误
WARN 3748 --- [nio-9000-exec-2] o.s.web.servlet.PageNotFound:不支持请求方法'GET'
感谢任何帮助。
PS:我假设因为我能够使用其他人调用WebService 模式,所以控制器很好。但是我的方式出了问题 从HTML中调用它。
以下HTML页面:
<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>
弹簧控制器
@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;
}
}
答案 0 :(得分:3)
我想解决方案在这里描述:Why SpringMVC Request method 'GET' not supported?
RequestMapping中的两个值必须相同。因此,对于每个值,您必须为GET定义一个,为POST定义一个。
@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/greetMe", method= RequestMethod.POST)
public @ResponseBody Greeting sayHello(
@RequestBody(required = true) Input input) {
return new Greeting(counter.incrementAndGet(),
String.format(getTemplate(input.getLanguage()), input.getName()));
}
答案 1 :(得分:1)
检查您的控制器,如果您已将任何这些调用映射到默认映射:
@DeleteMapping()
@PostMapping()
@GetMapping()
它映射到path="/"
。请将其更改为
@DeleteMapping(path="/something")
@PostMapping(path="/something")
@GetMapping(path="/something")