Вопрос по java – Получать статус HTTP после запроса с Spring MVC

4

я отправляю данные на сервер и хочу получить статус ответа HTTP, чтобы проверить этот статус и предоставить соответствующее представление

   @RequestMapping(method = RequestMethod.POST)
     public String Login(@ModelAttribute("Attribute") Login login, Model model,HttpServletRequest request) {

          // Prepare acceptable media type
          ArrayList<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
          acceptableMediaTypes.add(MediaType.APPLICATION_XML);

          // Prepare header
          HttpHeaders headers = new HttpHeaders();
          headers.setAccept(acceptableMediaTypes);

          HttpEntity<Login> entity = new HttpEntity<Login>(login, headers);

          // Send the request as POST
          try {
           ResponseEntity<Login> result = restTemplate.exchange("http://www.../user/login/", 
                   HttpMethod.POST, entity, Login.class);
          } catch (Exception e) {
          }
      //here i want to check the received status
      if(status=="OK"){
         return "login"
      }
      else          
      return "redirect:/home";
     }

Ваш Ответ

2   ответа
5

Error: User Rate Limit Exceeded

HttpStatus status = result.getStatusCode();
if(status == HttpStatus.OK)

Error: User Rate Limit ExceededResponseEntityError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded==Error: User Rate Limit Exceeded

status=="OK"

Error: User Rate Limit Exceeded

"OK".equals(status)

Error: User Rate Limit Exceeded

4

// Prepare acceptable media type
ArrayList<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_XML);

// Prepare header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);

HttpEntity<Login> entity = new HttpEntity<Login>(login, headers);
// Create status variable outside of try-catch block
HttpStatus statusCode = null;

// Send the request as POST
try {
  ResponseEntity<Login> result = restTemplate.exchange("http://www.../user/login/", 
  HttpMethod.POST, entity, Login.class);
  // Retrieve status code from ResponseEntity
  statusCode = result.getStatusCode();
} catch (Exception e) {
}
// Check if status code is OK
if (statusCode == HttpStatus.OK) {
  return "login"
}
else          
  return "redirect:/home";
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded../user/loginError: User Rate Limit Exceeded Alex Dowining
Error: User Rate Limit Exceededstackoverflow.com/questions/10507650/… Alex Dowining
Error: User Rate Limit Exceeded Alex Dowining
Error: User Rate Limit Exceeded

Похожие вопросы