Health Secretary Jeremy Hunt has won a High Court fight with junior doctors in England over a new contract.
The group Justice for Health mounted a legal bid arguing the contract was "unsafe and unsustainable" and Mr Hunt did not have the power to impose it.
But Mr Justice Green ruled the health secretary had acted "squarely" within his powers in what he did.
He clarified this as pushing for NHS trusts to introduce the contract, rather than compelling them.
The judge said "in principle" trusts had the freedom as employers to decide whether they did force the contract on doctors.
This prompted Justice for Health to claim the judgement had freed the profession from the "shackles" of Mr Hunt's timeline.
The new terms and conditions are due to start being rolled out next week - and there is no indication hospitals will not do this.
Mr Justice Green also considered whether Mr Hunt's actions lacked clarity and transparency and whether he acted irrationally in pursuing the new contract on the basis he believed it would help improve the quality of care at weekends.
On both counts, Mr Justice Green sided with the government.
He said Mr Hunt was entitled to believe changing staffing at weekends would have "some, material" impact.
The new contract changes the way doctors are reimbursed for weekend working.
Instead of Saturdays and Sundays being divided up between normal and unsocial hours, a system of supplements will be paid which depend on how many weekends a doctor works.
Ministers have insisted the contract is needed to improve levels of medical cover in hospitals at weekends so that the NHS can deliver seven-day services.
The dispute has led to six separate strikes by junior doctors, including the first all-out stoppages in the history of the NHS.
The British Medical Association had planned a run of five-day strikes in the four months leading up to Christmas, but recently called these off following concerns over patient safety.
In May, it looked as though a breakthrough had been reached in the dispute after both sides agreed to a new deal.
But the government announced in July that it would impose a new contract after junior doctors and medical students voted to reject the deal.
Responding to the verdict, a Department of Health spokeswoman said: "We must now move on from this dispute to the crucial job of making sure patients get the same high standards of urgent and emergency care every day of the week, which involves more than the junior doctors' contract.
"We urge the BMA to remove all threat of further industrial action so we can work constructively with junior doctors to address their wider concerns and better recognise their vital importance to the NHS."
Justice for Health was founded by five doctors and raised the money needed for the legal challenge through donations. The BMA had initially suggested it would also pursue legal action, but later dropped its case.