The colonial rampage embarked upon by European powers never uprooted the sovereignty of Japan; this is despite the somewhat close proximity of the island nation to a multitude of imperial possessions. How did it endure?
Simply put, Japan modernized. Quickly. They agreed to the trade treaties the United States forced upon them in 1853 and immediately began modernizing their army and navy.
The country was still under the control of the Tokugawa Shogunate in 1853, however the emperor wanted greater say in national affairs. This lead to the Boshin War in 1869, where the smaller but better equipped Imperial forces smashed the Shogunate. This lead to the restoration of the Imperial Throne and it affirmed that the country would modernize.
After the war's end, the Japanese government started bringing in British and German advisers to help with nation building, and began building the army from the ground up; new equipment, new organization, new doctrine, new everything. The Navy was styled after the British navy, it being the best in the world. Japan started a conscription military and began investing heavily in modern technology, schools and infrastructure. They also began setting up new systems of law based on European models, they helped create new businesses, etc. I can't stress enough how impressive the story of Japan's modernization is. They when from almost entirely undeveloped into a reasonably modern and powerful country in about 50 years.