A judge has ruled against a couple in China’s first-ever same-sex marriage lawsuit, in a case that advocates are nonetheless hailing as a step forward in the battle for gay and lesbian rights.

Sun Wenlin, a 26-year-old man living in the southern city of Changsha, late last year filed a lawsuit against local authorities for declining to register his marriage to his boyfriend, Hu Mingliang. In his complaint, Mr. Sun argued that China’s law defines marriage as between a husband and wife, but not specifically between a man and...