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

Java 如何在文本中反映传入的GET请求?,java,spring,rest,spring-mvc,spring-boot,Java,Spring,Rest,Spring Mvc,Spring Boot,我正在使用SpringMVC和Springboot(假设是最新版本)来建立一个简单的web服务,它返回一些文本 我能够弄清楚如何使用@RequestMapping和@PathVariable注释将其中一个URL路径变量显示为服务器的响应(例如,如果用户在浏览器中转到…/my_user_id/,则他们可以在浏览器中看到包含该用户id的某些文本,因为服务将其作为响应返回) 我需要帮助了解如何捕获用户浏览器发出的GET HTTP请求,然后将其以文本形式显示为浏览器中的响应(我希望以纯文本形式显示标题和

我正在使用SpringMVC和Springboot(假设是最新版本)来建立一个简单的web服务,它返回一些文本

我能够弄清楚如何使用@RequestMapping@PathVariable注释将其中一个URL路径变量显示为服务器的响应(例如,如果用户在浏览器中转到…/my_user_id/,则他们可以在浏览器中看到包含该用户id的某些文本,因为服务将其作为响应返回)

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

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

我尝试过的一种方法:

当我尝试上述方法时返回的错误的一些线程:

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

无论如何,当使用下面的@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();
    }
}
现在,当我尝试另一种方法(另一个@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;
}
}

编辑:如果主体为空,则使用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;
}
@RequestMapping(value=“/echoRequest”)
public@ResponseBody字符串echoRequest(HttpEntity HttpEntity,HttpServletRequest请求){
串出“”;
列表名称=req.getHeaderNames();
for(字符串名称:名称){
out+=(name+“:“+req.getHeader(name)+”\n”);
}
if(httpEntity.hasBody()){
out+=httpEntity.getBody();
}
返回;
}

举一个例子,说明您尝试过什么以及您试图实现什么,这会很有帮助。感谢@tanenbring我将添加我迄今为止尝试过的代码及其问题…感谢@tanenbring!我尝试将您的解决方案方法添加到我的SecondController.java(请参阅更新的问题描述中的代码),然后转到“”然后我得到的错误是------白标签错误页------此应用程序没有/错误的显式映射,因此您将此视为一个回退。------Mon Feb 08 17:38:38 EST 2016------出现意外错误(类型=错误请求,状态=400).----缺少必需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter@5c3f7f09“谢谢@tanenbring!效果很好。我只是稍微修改了您的代码,并将方法映射添加到了“/echoRequest”然后,导航到localhost:8080/site/echoRequest/返回浏览器的请求头(和正文,由于请求的性质,正文为空)。
@RequestMapping(value=“/echoRequest”)public@ResponseBody String echoRequest(HttpEntity HttpEntity,HttpServletRequest req){String out=“”;枚举headerNames=req.getHeaderNames();while(headerNames.hasMoreElements()){String key=(String)headerNames.nextElement();String value=req.getHeader(key);out+=(key+“:”+req.getHeader(key)+“\n”);}if(httpEntity.hasBody()){out+=httpEntity.getBody();}返回;}
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.)
@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;
}