A federal judge on Monday dismissed a defamation lawsuit filed by the pornographic film actress Stephanie Clifford against President Trump, ruling that the president had not defamed her on Twitter last spring and ordering her to pay his legal fees.

The tweet in question was posted by the president on April 18, one day after Ms. Clifford, known professionally as Stormy Daniels, posted a sketch of a man who, she alleged, threatened her in 2011 as she was first considering speaking out about the affair she said she had with Mr. Trump. Mr. Trump called the sketch “a total con job,” depicting “a nonexistent man.”

A sketch years later about a nonexistent man. A total con job, playing the Fake News Media for Fools (but they know it)! https://t.co/9Is7mHBFda

In its decision, the court sided with Mr. Trump’s lawyers’ argument that the tweet included an opinion, which the president was free to express.

“The court agrees with Mr. Trump’s argument because the tweet in question constitutes ‘rhetorical hyperbole’ normally associated with politics and public discourse in the United States,” Judge S. James Otero wrote in his decision. “The First Amendment protects this type of rhetorical statement.”

Hey, I’m Alex Burns, a politics correspondent for The Times. Send me your questions using the NYT app. I’ll give you the latest intel from the campaign trail.

Ms. Clifford’s lawyer, Michael Avenatti, had called the tweet an attack on his client’s credibility, arguing that Ms. Clifford should be compensated for what he called the resulting “harm to her reputation, emotional harm, exposure to contempt, ridicule, and shame,” along with threats to her physical safety.

The money now owed by Ms. Clifford to compensate Mr. Trump’s team of lawyers for their work on the case over nearly six months will not be insignificant. While Charles J. Harder, a lawyer for Mr. Trump, said Monday that the fees had yet to be determined, the case required several lawyers and multiple court filings. At least four lawyers represented the president at a recent court hearing in Los Angeles, and one filing drew on research that cited more than three dozen cases as precedent.

The defamation suit — filed last April in federal court in Manhattan and later transferred to California at the request of Mr. Trump’s lawyers — stands apart from Mr. Avenatti’s efforts to nullify the nondisclosure agreement that Ms. Clifford signed days before the 2016 election, agreeing to keep quiet about the affair that Ms. Clifford said occurred in 2006. Mr. Trump, whose statements about that nondisclosure agreement and the $130,000 payment that went along with it have changed over the course of the year, has denied there was an affair.

On Twitter on Monday, Mr. Avenatti emphasized his continuing litigation relating to Mr. Trump and his former personal lawyer, Michael Cohen, who in August pleaded guilty to multiple federal crimes and implicated the president in violating campaign-finance law. “We will appeal the dismissal of the defamation cause of action and are confident in a reversal,” he wrote. He added, “There is something really rich in Trump relying on the First Amendment to justify defaming a woman.”

In a statement on Monday, Mr. Harder, the lawyer for the president, said, “No amount of spin or commentary by Stormy Daniels or her lawyer, Mr. Avenatti, can truthfully characterize today’s ruling in any way other than total victory for President Trump and total defeat for Stormy Daniels.”

A version of this article appears in print on , Section A, Page 15 of the New York edition with the headline: Actress’s Defamation Lawsuit Against Trump Is Dismissed. Order Reprints | Today’s Paper | Subscribe