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

时间:2016-02-08 20:07:51

标签: java spring rest spring-mvc spring-boot

我使用Spring MVC和Springboot(假设最新版本)来建立一个返回一些文本的简单Web服务。

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

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

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

我尝试过的方法:

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

更多关于Spring MVC,我大量使用:

总之。当使用下面的@Controller时,我收到一个错误:

myObj.Prop = AsEnum<MyEnum>("value") ?? MyEnum.Default;
//versus 
SetPropery<MyObject, MyEnum>(myobj, (r, e) => r.Prop = e, "value");

运行此代码并导航到localhost:8080 / request_mirror / stuff3 /时,出现以下错误:

@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)时 - 代码如下:

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

对于上面的代码(SecondController),(来自http://www.mkyong.com/java/how-to-get-http-request-header-in-java/),当我尝试导航到localhost时出现以下错误:8080 / site / stuff123456789 ...(但我可以看到标题检查时Map中请求的键和值...只是不确定如何在浏览器中将它们显示为文本作为响应)。

@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;
}
}

1 个答案:

答案 0 :(得分:1)

编辑:使用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;
}