BEIJING — Defense Secretary Jim Mattis held talks with Chinese officials on Wednesday, marking the first face-to-face encounter between leaders of the countries’ militaries since the United States declared Beijing to be a primary challenge to American security.

The visit, the first by an American defense chief to China since 2014, is an early test of how a new era of competition with what the Pentagon describes as a chief “revisionist power” will shape U.S.-China ties and, American officials hope, help the United States preserve its military edge.

Speaking alongside President Xi Jinping at the Great Hall of the People, Mattis sought to telegraph American objectives in Asia in hopes of defusing mounting tensions over issues including Taiwan, China’s buildup in the South China Sea and President Trump’s trade policy.

“I’m here to keep our relationship on the right trajectory,” Mattis told Xi, “to share ideas with your military leadership as we look at the way ahead.”

But in an indication of the wide gap that remains, China issued a warning that “any inch of territory passed down from ancestors cannot be lost while we want nothing from others,” Xinhua news agency later reported Xi told Mattis. “Our stance is steadfast and clear-cut when it comes to China’s sovereignty and territorial integrity.”

The visit comes as positions harden among U.S. Republicans and Democrats against China, whose investments in advanced military hardware and sophisticated technology now threaten the United States’ decades of global military superiority.

It also coincides with a period of contradictory messages from Washington, as the Trump administration risks a trade war by slapping new tariffs on Chinese goods and disinvites Beijing from a major naval exercise while at the same time seeking diplomatic support for its effort to denuclearize China’s neighbor North Korea.

“The question that is going through Chinese leaders’ heads at this time is: How do we interact with the U.S. military in an era that’s defined by revisionism and strategic competition?” said Abigail Grace, a former White House official now at the Center for a New American Security.

During his nearly 48-hour visit, Mattis also met with Defense Minister General Wei Fenghe and Yang Jiechi, a senior foreign policy adviser to the president.

U.S. officials said the talks included passing reference to the trade war brewing with China, as new U.S. tariffs are poised to take effect and Beijing promises to strike back at the U.S. economy. But the goal of the talks, they said, was to convey U.S. interests in Asia so the two countries can avoid escalating friction akin to what followed the collision of a U.S. spy plane with a Chinese jet in 2001.

Dana White, chief Pentagon spokeswoman, said the talks touched on issues including Washington’s condemnation of the placement of Chinese military assets in areas of the South China Sea also claimed by U.S. allies in the region.

“It’s not for one country to diminish what are international rights for navigation,” she said, adding other areas of disagreement were also “identified but not necessarily dwelled on.”

U.S. officials have stressed their hope to expand emerging U.S.-Chinese military cooperation and, more urgently, broaden communications channels that might prevent conflicts when the two militaries unexpectedly come into contact in the air or at sea.

Song Zhongping, a Hong Kong-based Phoenix TV commentator and former member of the Second Artillery Corps of the People’s Liberation Army (PLA), said military cooperation between the two countries had been limited, and he questioned the possibility for consensus. “The problems between China and the United States are structural and those problems won’t be solved by a meeting or two,” he said.

Moreover, the two countries often fail to get beyond repeating their respective positions during dialogues, said Michael Kovrig, an analyst at the International Crisis Group.

“The danger now is that if each side sees the other as a strategic competitor and acts accordingly, they will pursue harmful measures and countermeasures, and tensions could intensify in a negative spiral,” he said.

The Chinese government appeared to take umbrage at the characterization of it as a revisionist power threatening the United States, laid out in a National Defense Strategy unveiled by Mattis earlier this year.

“I don’t think they like the description, but from the secretary’s view[point] this is where they start the conversation; they start it from a reality basis,” the defense official said, speaking on the condition of anonymity to characterize sensitive discussions.

The advances by China’s People’s Liberation Army that have most worried American officials include the planned construction of aircraft carriers and other naval assets; an increasingly robust arsenal of ballistic missiles, including anti-ship missiles; a fleet of advanced aircraft and a growing capability to challenge U.S. dominance in space.

China has also opened its first overseas base, in the East African nation of Djibouti, and invested in its cyberwarfare ability.

Speaking in Washington last week, Air Force Gen. Paul Selva, vice chairman of the Joint Chiefs of Staff, said the Pentagon was working on ways to respond to China’s desire to achieve technological superiority within 15 years, for example, by investing in high-speed long-range precision strike weapons.

The Pentagon will use a share of what it expects will be a substantial boost in defense spending over coming years to counter the challenge from China and Russia, another nation it has labeled a revisionist power.

Luna Lin contributed to this report.