标签: httpresponse

从 HttpSessionEvent 获取 Request 对象

我有一个Session扩展的监听器PortalSessionListener。我有sessionCreated(HttpSessionEvent httpSessionEvent)sessionDestroyed(HttpSessionEvent httpSessionEvent)方法

当我的Session监听器失效时(根据我在 web.xml 中的配置,15 分钟后),我的监听器将被调用并Session失效。

Cookie在我的监听器中,我想在注销用户之前清除值。所以,我想要RequestResponse对象,以便我可以清除 Cookie 值并将其设置为Response.

Request / Response但是,如何在侦听器中获取具有 的对象HttpSessionEvent

我尝试了下面的代码。sessionDestroyed但是,当调用我的方法或任何其他阶段时,这不会被调用。

public void requestInitialized(ServletRequestEvent servletRequestEvent) 
 {
        log.debug("Entered into requestInitialized method");
        HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();

        log.debug("Request object created is :" +request);

 }
Run Code Online (Sandbox Code Playgroud)

有人建议实现 aFilter适合此要求(用于获取Request对象)。如何将其应用到我的场景中?

session httpresponse httprequest

0
推荐指数
1
解决办法
7148
查看次数

未找到搜索结果的正确 HTTP 状态代码是什么?

我们正在用 Java 实现 REST API。

对于 GET REST 端点,如果我搜索某些内容:

  1. 如果找到任何记录/记录,则它将返回 200 状态代码和结果/结果。

  2. 如果没有找到记录,正确的 HTTP 状态代码是什么?

当我在 Google 和 SO 中搜索时,我发现了不同的答案,例如:

200 with empty list (if the API response is item but NOT list, then I can't send empty list)
204 No Content
404 Not Found
Run Code Online (Sandbox Code Playgroud)

如果未找到记录或未找到搜索结果,正确的 HTTP 状态代码是什么?

rest http httpresponse spring-boot

0
推荐指数
1
解决办法
2936
查看次数

Spring Boot ResponseEntity 不操纵 HTTP 响应

我在响应实体异常处理方面遇到了问题。正如所见,我的响应实体错误没有改变 HTTP 响应。

我的代码

      public ResponseEntity<User> retriveUser(@PathVariable int id){
      Optional<User> foundUser;
      foundUser= userRepo.findById(id);
      
      if(foundUser.get()==null) {
          return new ResponseEntity<>(foundUser.get(),HttpStatus.HttpStatus.NOT_FOUND);
          }
      
      else {
          return new ResponseEntity<>(foundUser.get(),HttpStatus.OK);
          }    
  }  
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

error-handling spring httpresponse http-response-codes spring-boot

0
推荐指数
1
解决办法
303
查看次数

springboot项目中不显示异常错误信息

我尝试通过使用 RuntimeException 扩展我的类来显示我的服务模型的不可用资源的简单错误消息,但没有显示

Todo服务实施

@Override
public String retrieveTodoStatusById(Long id) {
    Optional<String> optionalTodo = Optional.ofNullable(todoRepository.findStatusById(id));
    System.out.println("OptionalTodo " +optionalTodo );
    String status = optionalTodo.orElseThrow(TodoNotFoundException::new);
    return  status;
}
Run Code Online (Sandbox Code Playgroud)

TodoNotFound异常

package mang.io.todosrestapi.exceptionhandler;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Todo not available")
public class TodoNotFoundException extends RuntimeException{

    public TodoNotFoundException(){
    }

public TodoNotFoundException(String message){
    super(message);
}
Run Code Online (Sandbox Code Playgroud)

}

默认错误,不显示任何消息

每次运行异常错误信息都不显示 如何显示错误信息?

java api exception httpresponse spring-boot

0
推荐指数
1
解决办法
1448
查看次数

Golang 使用响应中的正文更改 HTTP 响应状态代码 201

在响应正文中包含 JSON 对象时,如何将响应状态代码设置为 201?我似乎无法同时执行这两项操作——要么返回带有消息正文的 200,要么返回没有消息正文的 201。

http httpresponse go http-status-code-404

-1
推荐指数
1
解决办法
6234
查看次数