3 回答

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
在您的請(qǐng)求中包含 HttpServlet響應(yīng)。這將在運(yùn)行時(shí)設(shè)置標(biāo)頭。例如:
@RequestMapping(value = "/login") public String hello(@RequestHeader(value="LIB_AUTH_TOKEN") String token, HttpServletResponse aResponse)

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
執(zhí)行:
curl -i -H "LIB_AUTH_TOKEN: test123" -X GET http://localhost:8080/login
下面與POST和GET一起工作正常,只需添加一個(gè)響應(yīng):
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/login")
public String hello(@RequestHeader(value="LIB_AUTH_TOKEN") String token){
System.out.println(token);
return "hi";
}
}
更新
指定 HTTP 方法:
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String hello(@RequestHeader(value="LIB_AUTH_TOKEN") String token){
System.out.println(token);
return "hi";
}
}

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
我有類似的問(wèn)題,但在我的情況下,我希望它是可選的。問(wèn)題是,默認(rèn)情況下,@RequestHeader的 require 屬性為 true(我假設(shè)它在默認(rèn)情況下為 false)
希望這將節(jié)省某人的時(shí)間!
添加回答
舉報(bào)