Mr. Xi’s tough stance against South Korea also included a punishing economic boycott that helped reinforce the American relationship with Seoul, undermining China’s long-term goal of replacing the United States as the pre-eminent power in Asia.
“This is the reversal of an ineffective and costly policy on the part of China,” said Shi Yinhong, professor of international relations at Renmin University of China.
In agreeing to restore cordial relations, South Korea pledged not to accept additional Thaad launchers, and agreed not to join a regional missile defense system with the United States and Japan. The agreement not to accept any more Thaad deployments had been a longstanding policy stance of Mr. Moon anyway, a South Korean government official said on Wednesday.
South Korea also promised not to join a trilateral military alliance with the United States and Japan. Mr. Moon, like his predecessors, had shown no interest in expanding military relations with Japan, its former colonial master.
With the increased threat from North Korea, Mr. Moon had aligned himself more closely with Mr. Trump and Prime Minister Shinzo Abe of Japan.
The three leaders met on the sidelines of the Group of 20 summit meeting in Germany in July, and agreed to enhance their defense capabilities against the North Korean threat.
In warming up to South Korea, Mr. Xi probably recognized that Mr. Moon would be more malleable to favoring dialogue with North Korea than was his conservative predecessor, Park Geun-hye.
At the recent party congress in which he was elevated to a second five-year term as president, Mr. Xi showed himself determined to project China’s power in a “new era” of China astride the world stage. Resolving the North Korea crisis dovetails with that theme, and any move toward talking with the North would be easier with Mr. Moon by his side.
“South Korea believes that China still has an influence over North Korea and an important role to play in resolving its nuclear problem,” said Lee Dong-ryul, professor of Chinese studies at Dongduk Women’s University in Seoul.
South Korea and China announced their decision to restore relations just ahead of President Trump’s first visit to Asia, which includes stops in Seoul and Beijing.
The timing was interpreted in Beijing as a way to blunt some of the impact of the American president’s stop in Seoul, where he is expected to deliver a speech to the National Assembly.
“It could be seen as a pre-emptive response to Trump’s visit,” Mr. Shi said.
Indeed, the rapprochement between China and South Korea carries risks for the United States. How far Mr. Moon would now lean toward China is something that Washington needs to watch closely, said Evans J. R. Revere, a former State Department official who has dealt with the Korean Peninsula.
In agreeing not to join a regional missile defense system, South Korea is addressing China’s concerns about what it views as the United States’ aim to “contain” China.
“Beijing was worried that Thaad would eventually be succeeded by ‘son of Thaad’ — a regional missile defense system involving the United States, South Korea and Japan and others that would be aimed at dealing with China’s offensive missile force, unlike the current Thaad, which it is not,” Mr. Revere said.
For Mr. Moon, China’s fierce economic boycott of popular South Korean goods as punishment for the Thaad deployment has taken a toll. China is by far the biggest trading partner of South Korea; two-way trade is bigger than South Korea’s trade with the United States and Japan combined.
The Hyundai Research Institute found that the Thaad dispute was likely to cost South Korea $7.5 billion so far this year, a 0.5 percent hit on its gross domestic product. China, for its part, lost $880 million, just a 0.01 percent drop of its G.D.P., the institute said.
South Korean car sales plummeted in China. Lotte, the retailer, recently put 112 of its stores in China on the market after customers abandoned it. South Korean movies and cosmetics also suffered.
The boycott — coupled with what was perceived as China’s interference in South Korea’s internal affairs over Thaad — hardened the view of China as a bully among the South Korean people.
“We have seen anti-Chinese sentiments rising in South Korea,” said Seo Jeong-kyung, professor at the Sungkyun Institute of China Studies in Seoul. “So did the approval ratings for the Thaad deployment, and calls mounted for strengthening the alliance with the Americans.”
China did not offer a timetable for improving economic relations, although the accord said the “normal track” would be achieved “expeditiously.”
Despite the apparent resolution of the standoff between the two countries, there was no guarantee that the accord would stick.
People’s Daily, the official newspaper of the Communist Party, issued a somewhat friendly, but mostly stern, editorial. “Only proper resolution of the Thaad issue can bring the Sino-Korean relationship back onto the right track,” it said.
It was possible that both sides agreed to resolve their differences so the two leaders, Mr. Xi and Mr. Moon, could meet in Vietnam next week during an Asian economic summit meeting. After that, there is talk of Mr. Moon visiting China before the end of the year.
“This is a direct result of South Korea’s efforts to mend fences,” said Cheng Xiaohe, an associate professor of international relations at Renmin University. “China also realizes that Thaad should not hold hostage the whole relations between the two nations. But I think the Thaad issue is just shelved, not resolved.”