The 322-to-87 vote is the first time a chamber of Congress has agreed to override one of Mr. Trump’s vetoes, underscoring the sweeping popularity of the military legislation, which authorizes a pay raise for the nation’s troops. It also amounted to a remarkable reprimand over the president’s decision to flout one of his party’s key orthodoxies — projecting military strength — from Republicans who have been reluctant to challenge Mr. Trump during his four years in office.

The margin surpassed the two-thirds majority needed in both houses to force enactment of the bill over Mr. Trump’s objections. The Senate will take up the legislation later in the week, and it is expected to pass.

“The president has exercised his constitutional prerogative. Now, Madam Speaker, it’s up to us,” Representative Mac Thornberry of Texas, the top Republican on the House Armed Services Committee, said in remarks before the vote. “Our troops, the country, indeed, the world is watching to see what we will do — whether we can tune out other differences and still come together to support the men and women of the military and American national security.”