我使用Spring和Springboot (假设是最新版本)来安装一个简单的web服务,返回一些文本。
我想出了如何使用@RequestMapping和@PathVariable注释来显示一个URL路径变量作为服务器的响应(例如,如果用户转到./my_ user _id/在浏览器中,他们可以在浏览器中看到一些包含该user_id的文本.因为服务返回它作为响应)。
我需要帮助解决如何捕获用户浏览器发出的GET HTTP请求,然后将其以文本形式显示为浏览器中的响应(我希望以纯文本形式显示标题和请求正文)。
我做了一些研究,但是没有一个解决方案能正常工作。有人知道这里的正确方法/可行性吗?
我尝试过一种方法:
当我尝试上述方法时,会得到一些关于错误的线程:
关于Spring的更多信息,我正在大量使用它:
不管怎么说。当使用下面的@Controller时,我会得到一个错误:
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Welcome to your home directory";
}
@RequestMapping(value="/mydata/{userId}", method=RequestMethod.GET)
public String printTheUser(@PathVariable String userId) {
return "The data for " + userId + " would live here";
}
@RequestMapping("/summary_data/")
public String index3() {
return "All summary data would appear here";
}
private String server = "localhost";
private int port = 8080;
@RequestMapping("/request_mirror/**")
public @ResponseBody String mirrorRest(@RequestBody String body, HttpMethod method, HttpServletRequest request,
HttpServletResponse response) throws URISyntaxException {
URI uri = new URI("http", null, server, port, request.getRequestURI(), request.getQueryString(), null);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity =
restTemplate.exchange(uri, method, new HttpEntity<String>(body), String.class);
return responseEntity.getBody();
}
}当运行此代码并导航到localhost:8080/request_镜像/stuff3 3/时,我得到以下错误:
Whitelabel Error Page. This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Feb 08 15:41:13 EST 2016
There was an unexpected error (type=Bad Request, status=400).
Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@a35a9b3f现在,当我尝试另一种方法(另一个@Controller)时,代码如下所示:
@Controller
@RequestMapping("/site")
public class SecondController{
@Autowired
private HttpServletRequest request;
@RequestMapping(value = "/{input:.+}", method = RequestMethod.GET)
public ModelAndView getDomain(@PathVariable("input") String input) {
ModelAndView modelandView = new ModelAndView("result");
modelandView.addObject("user-agent", getUserAgent());
modelandView.addObject("headers", getHeadersInfo());
return modelandView;
}
//get user agent
private String getUserAgent() {
return request.getHeader("user-agent");
}
//get request headers
private Map<String, String> getHeadersInfo() {
Map<String, String> map = new HashMap<String, String>();
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String key = (String) headerNames.nextElement();
String value = request.getHeader(key);
map.put(key, value);
}
return map;
}
}对于上面的代码(SecondController),(来源于http://www.mkyong.com/java/how-to-get-http-request-header-in-java/),当我试图导航到本地主机时,我会得到以下错误:8080/site/stuff123456789.(但我可以在检查时看到Map中的请求中的头键和值.只是不知道如何将它们作为响应在浏览器中显示为文本)。
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Feb 08 16:10:47 EST 2016
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [stuff123456789]: would dispatch back to the current handler URL [/site/stuff123456789] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)发布于 2016-02-08 20:28:26
编辑:使用HttpEntity获取身体,以防它是空的。
我不知道你到底想达到什么目的,但我认为这可能是近在咫尺:
@RequestMapping(value="/echoRequest")
public @ResponseBody String echoRequest(HttpEntity<String> httpEntity, HttpServletRequest req) {
String out = "";
List<String> names = req.getHeaderNames();
for (String name : names) {
out += (name + ": " + req.getHeader(name) + "\n");
}
if (httpEntity.hasBody()) {
out += httpEntity.getBody();
}
return out;
}https://stackoverflow.com/questions/35278356
复制相似问题