Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring Boot中设置index.html上的cookie或标头_Java_Spring_Spring Boot_Header_Vue.js - Fatal编程技术网

Java 如何在Spring Boot中设置index.html上的cookie或标头

Java 如何在Spring Boot中设置index.html上的cookie或标头,java,spring,spring-boot,header,vue.js,Java,Spring,Spring Boot,Header,Vue.js,我正在开发一个包含vue.js代码的spring启动应用程序 现在在初始上下文“/”中,我想在index.html页面上设置cookie或标题 我已经尝试了下面的代码,我一直得到一个404。有没有办法解决这个问题 @Controller public class TestController { @RequestMapping(path = "/", method = RequestMethod.GET) public String index(HttpServletRequest httpS

我正在开发一个包含vue.js代码的spring启动应用程序

现在在初始上下文“/”中,我想在index.html页面上设置cookie或标题

我已经尝试了下面的代码,我一直得到一个404。有没有办法解决这个问题

@Controller
public class TestController {

@RequestMapping(path = "/", method = RequestMethod.GET)
public String index(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws IOException {

    String reqUrl = httpServletRequest.getRequestURL().toString();
    String displayName = httpServletRequest.getHeader("displayName");

    if (StringUtils.isBlank(displayName)) {

        String url = "http://example.com/sso/?targetUrl=" + reqUrl;

        URL obj = new URL(url);

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        System.out.println("Header from SSO" + con.getHeaderField("displayName"));
        httpServletResponse.setHeader("displayName", con.getHeaderField("displayName"));
    }

    return "index";
}

}
这就是我的项目结构的样子


看起来您正试图从刚刚创建的HttpURLConnection中获取标头


如果您试图中继到“”url,则最好使用httpClient。

我解决了问题,必须将返回语句更改为

return "index.html";