How should I create a controller which is thread safe?
As per the best practice controllers are singleton.
Consider the below code in which im storing user data through a autowired service Object ,which makes my code stateful. How would i make the below code thread safe.
@RestController
class ApiController {
@Autowired
IDbService< User > iDBService;
@RequestMapping(value = "/api/adduser", method = RequestMethod.POST)
public ResponseEntity<User> createUser(@RequestBody User user){
User savedUser=iDBService.create(user);
return new ResponseEntity<User>(savedUser, HttpStatus.CREATED);
}
Here is my Service implementation. I have shared variable in my service
public class IDbServiceImpl<T> implements IDBService<T>{
@Autowired
GenericRepository<T, Serializable> genericRepository;
@Override
public T create(T object) {
return genericRepository.save(object);
}
}