I have a requirement where I need to call aspect before processing the rest endpoint method, I have created an annotation and annotating the rest endpoint,
I am able to process the request but when reading InpuStream it is already closed inseide aspect. The code snippet is attached I am using spring boot 1.5
Once I get input stream in the Aspect I will cache it before using it, but my problem is at the current code it is throwing an
io exception stream is closed
at line
Map jsonMap = mapper.readValue(request.getInputStream(), Map.class);
Application
@SpringBootApplication
@ComponentScan(basePackages = { "org.ap" })
public class UserAuthPocApplication {
public static void main(String[] args) {
SpringApplication.run(UserAuthPocApplication.class, args);
}
}
Annotation
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface HasPrivilegeXX {
}
Aspect
@Aspect
@Component
public class MyPrivilegeAspect {
@Before("@annotation(HasPrivilegeXX)")
public boolean logBeforeAllMethods(JoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();
try {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonMap = mapper.readValue(request.getInputStream(), Map.class);
System.out.println("In aspect " + jsonMap.toString());
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
Rest Endpoint
@RestController
@RequestMapping("/api")
public class Api {
@PostMapping("/hi")
@HasPrivilegeXX()
public String hiPost(@RequestBody User user) {
System.out.println(user.toString());
return "Hi...";
}
}
User
public class User {
private String name;
private String company;
private Integer id;
}