Yes, you can do this. All you're going to need is a, 'step-down' CONVERSION barrel from a Glock parts supplier like Lone Wolf Distributors. You may, or may not, need to convert several other parts like: extractors, ejectors, SLB's, SLB springs, or entire magazines. There are no hard fast rules for what must be changed in order to make a conversion Glock work (reasonably) well; it really depends on your individual Glock. What you don't want to ever do, though, is have to use a converted Glock pistol for any sort of serious self-defense work. Step-down caliber conversions are primarily for inexpensive practice at the range and not much else.
Just so you know: There isn't a lot of difference between modern 40 caliber and 9mm ammunition. For all practical intents the one hits as hard as the other.