Skip to content

Instantly share code, notes, and snippets.

@Charlynux
Created September 29, 2018 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Charlynux/5d845f966e33bbfef212c2a1db669b3c to your computer and use it in GitHub Desktop.
Save Charlynux/5d845f966e33bbfef212c2a1db669b3c to your computer and use it in GitHub Desktop.
// Package definition and imports
@Controller
public class LoggingController {
@GetMapping("/")
public String readValues(Request request) {
String cookieValue = readCookieValue(request);
String headerValue = readHeaderValue(request);
// Do something with values
}
private String readCookieValue(Request request) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("cookieName")) {
return cookie.getValue();
}
}
}
return "";
}
private String readHeaderValue(Request request) {
String headerValue = request.getHeader("headerName");
if (headerValue == null) {
return "";
} else {
return headerValue;
}
}
}
// Package definition and imports
@Controller
public class LoggingController {
@GetMapping("/")
public String readValues(
@CookieValue(value = "cookieName", defaultValue = "") cookieName,
@RequestHeader(value = "headerName", defaultValue = "") headerName
) {
// Do something with values
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment