gc5*_*gc5 2 spring json hibernate servlets jackson
我正在尝试使用 JSON 负载通过 ajax POST 发送对象;这个对象有对存储在数据库中的其他对象的引用,由 Hibernate 处理;我需要访问这个数据库来解析其他对象引用并将它们存储在反序列化请求的 JSON 有效负载的新对象中。
现在,我必须访问 HttpServletRequest 属性才能获得保存的休眠会话以用于访问数据库。是否可以?
处理请求的控制器如下:
@RequestMapping(value = "/newproduct", method = RequestMethod.POST)
public @ResponseBody
Integer newProduct(HttpServletRequest request, @RequestBody Product product)
{
//Controller code here
}
Run Code Online (Sandbox Code Playgroud)
我必须能够获取请求属性“hibernate_session”的解串器是一个自定义解串器,已注册到 Jackson,如下所示:
public class ProductDeserializer extends JsonDeserializer<Product>
{
@Override
public Product deserialize(JsonParser jpar, DeserializationContext arg1)
throws IOException, JsonProcessingException
{
Product newProduct = new Product();
// I want to get request attribute or open a new hibernate session here
return newProduct;
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要,我会发布更多代码。
谢谢
您可以尝试以下方法
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
Run Code Online (Sandbox Code Playgroud)