你好我是Spring,Rest和servlets的新手。我构建了一个示例,其中包含一个从用户获取3个参数并在浏览器中打印它们的函数。 不,我执行以下命令:
http://localhost:8080/springexample/getMails/setup/user/1234/matant@gmail/
我得到:
HTTP Status 400 - Required String parameter 'username' is not present
type Status report
message Required String parameter 'username' is not present
description The request sent by the client was syntactically incorrect.
这是我的代码:
package com.javacodegeeks.snippets.enterprise;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/getMails")
public class GetEmail {
@RequestMapping(value = "/setup/{username}/{pass}/{host}/", method = RequestMethod.GET)
public String Setup(@RequestParam("username") String username,@RequestParam("pass") String pass,@RequestParam("host") String host ,ModelMap model,HttpServletRequest request) {
model.addAttribute("msg","user = "+username+","+"pass = "+pass+","+"host = "+host);
return "helloWorld";
}
@RequestMapping(value = "/displayMessage/{msg}", method = RequestMethod.GET)
public String displayMessage(@PathVariable String msg, ModelMap model) {
model.addAttribute("msg", msg);
return "helloWorld";
}
@RequestMapping(value = "/knainz", method = RequestMethod.GET)
public String knainz(ModelMap model) {
model.addAttribute("msg", "JCG Hello World2222!");
return "helloWorld";
}
@RequestMapping(value = "/func1/{message}", method = RequestMethod.GET)
public String func1(@PathVariable String message,ModelMap model) {
model.addAttribute("msg", message);
return "helloWorld";
}
}
答案 0 :(得分:1)
您将@RequestParam
与@PathVariable
混淆。
变化
@RequestMapping(value = "/setup/{username}/{pass}/{host}/", method = RequestMethod.GET)
public String Setup(@RequestParam("username") String username,@RequestParam("pass") String pass,@RequestParam("host") String host ,ModelMap model,HttpServletRequest request) {
model.addAttribute("msg","user = "+username+","+"pass = "+pass+","+"host = "+host);
return "helloWorld";
}
到
@RequestMapping(value = "/setup/{username}/{pass}/{host}/", method = RequestMethod.GET)
public String Setup(@PathVariable("username") String username,@PathVariable("pass") String pass,@PathVariable("host") String host ,ModelMap model,HttpServletRequest request) {
model.addAttribute("msg","user = "+username+","+"pass = "+pass+","+"host = "+host);
return "helloWorld";
}