首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文本中反映传入的GET请求?

如何在文本中反映传入的GET请求?
EN

Stack Overflow用户
提问于 2016-02-08 20:07:50
回答 1查看 1.7K关注 0票数 2

我使用Spring和Springboot (假设是最新版本)来安装一个简单的web服务,返回一些文本。

我想出了如何使用@RequestMapping@PathVariable注释来显示一个URL路径变量作为服务器的响应(例如,如果用户转到./my_ user _id/在浏览器中,他们可以在浏览器中看到一些包含该user_id的文本.因为服务返回它作为响应)。

我需要帮助解决如何捕获用户浏览器发出的GET HTTP请求,然后将其以文本形式显示为浏览器中的响应(我希望以纯文本形式显示标题和请求正文)。

我做了一些研究,但是没有一个解决方案能正常工作。有人知道这里的正确方法/可行性吗?

我尝试过一种方法:

  • http://www.mkyong.com/java/how-to-get-http-request-header-in-java/

当我尝试上述方法时,会得到一些关于错误的线程:

关于Spring的更多信息,我正在大量使用它:

  • http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

不管怎么说。当使用下面的@Controller时,我会得到一个错误:

代码语言:javascript
复制
@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/时,我得到以下错误:

代码语言:javascript
复制
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)时,代码如下所示:

代码语言:javascript
复制
@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中的请求中的头键和值.只是不知道如何将它们作为响应在浏览器中显示为文本)。

代码语言:javascript
复制
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.)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-08 20:28:26

编辑:使用HttpEntity获取身体,以防它是空的。

我不知道你到底想达到什么目的,但我认为这可能是近在咫尺:

代码语言:javascript
复制
@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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35278356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档