There was no actual foreign invasion of Japan during its Medieval period; however the fear of an invasion was a substantial factor.
The early medieval period is known as the time of the sengoku, or "country at war." Although Japan had an emperor, he was only a figurehead with no real power. The true power was the Shogun, or military ruler, who shared power in an uneasy relationship with the Daimyo, or feudal lords, each of whom ruled absolutely in his own domain. When the Shogun learned of the European invasion of the Philippines, he quickly deduced that any of the daimyo might form an alliance with the Europeans or at a minimum secure superior European weapons from them. There was also the possibility of an outright invasion. As a purely preventative measure, the Shoguns issued a number of edicts in the seventeenth century which effectively closed Japan to foreigners. Foreign travel was an offense punishable by death; all Europeans living in Japan were required to leave immediately and all foreign publications were prohibited. The only exceptions were the occasional Chinese or Dutch ship whose activities were closely governed. When a Portuguese ship arrived in an attempt to open trade, the a substantial number of the crew were beheaded. The crews of other ships which attempted entry were often crucified. Some few were released only to carry the message that Japan was closed to foreigners. This fear of foreign intervention or outright invasion left Japan closed to Europe and most other foreigners for several hundred years.
In fact the Mongols, under Kublai Kahn, attempted two invasions of Japan, first in 1274 and then again in 1281.
For the first invasion the Mongols had underestimated the fighting prowess of the Samurai and had sent a relatively small invasion force of 25,000 Mongols (plus a smaller number of Korean soldiers and sailors). After several fierce engagements, the Mongols, surprised by the level of resistance and now running low on basic supplies (like arrows) decided to withdraw. It was at this point that a winter storm caught the invasion fleet as it put out to sea. Over 13,000 were lost during this invasion, most by drowning.
After Kublai Kahn completed his conquest of Southern China he once again turned his attention to Japan and mounted a second, and much larger invasion in 1281. This time two fleets would attack Japan, the smaller from Korea containing 10,000 Korean Soldiers and 15,000 Chinese and Mongol warriors, while a larger fleet from Southern China would bring 3,500 ships carrying 100,000 soldiers and 60,000 sailors.
However, the Samurai had learned from the first invasion and they were ready.
Once again the Mongols were surprised at just how fierce the resistance was and losses were heavy among the assault troops. In fact, at the main invasion site in Hakata Bay, " ... only one unit was able to land even after several days of continuous fighting." The Samurai also launched ship borne raids against the Mongol Fleet, destroying several ships and taking many heads. These feats served to boost the defender's morale, while forcing the invaders to remain on their crowded ships where over 3,000 died of disease as the ships themselves began to rot.
It was at this point that the larger fleet arrived and the Japanese realized that the decisive battle was about to begin. Although their valor had served them well, they knew it would not be enough. So the retired Emperor Kameyama sent envoys to Ise Shrine to petition his ancestor, the sun goddess Ameterasu, for divine help.
That very day, August 15, 1281, "...there appeared in the sky a cloud, no bigger, perhaps, than a man's hand. The cloud grew and spread, until an early and lurid darkness blotted out the setting sun over the Genkai Sea where the Mongol armada lay." The ensuing storm was so powerful that the invasion fleet was destroyed at anchor, the Chinese fleet alone losing more than 50,000.
Dubbed the kamikaze or 'divine wind' the "...effect on Japanese pride was colossal, for the kamikaze was literally regarded as a weapon from heaven and from that time onwards the kamikaze came to be seen as a symbol of Japan's divine protection, and the shrine of Ise earned more honor and respect than ever before."
(All quoted passages are from The Samurai - A Military History by S.R Turnbull, c1977 pages 84-94.)