The European court of justice (ECJ) has ruled that Germany can refuse welfare benefits to EU migrants if they have never held a job in the country.

In a landmark ruling on “benefit tourism” that could set an EU-wide legal precedent, the Luxembourg court announced on Tuesday that “economically inactive” migrants from other EU nations can be refused German unemployment benefits under certain conditions.

David Cameron’s office said it would look very closely at the ruling and argued that it vindicated the prime minister’s moves to restrict benefits to EU migrants.

“I think one of the things that it underlines has been that the freedom of movement, as the prime minister and others have said, is not an unqualified right,” Cameron’s spokesman said. “We will look very carefully at what we and other governments can do working together in response to this judgment.”

Asked whether this will make it easier for the government to bring in further controls, the spokesman said: “We have already said prior to this judgment that we are looking to see what further measures can be taken and following that judgment that work will continue.”

The case was prompted after Elisabeta Dano, a 25-year-old Romanian woman living in Leipzig, had her application for benefits refused. The local jobcentre argued that there was a lack of evidence to prove that the woman, who has lived in Germany since 2010, had ever actively looked for work.

After an appeal was rejected by a Leipzig social court, the case was transferred to the ECJ in Luxembourg.

In its ruling, the ECJ emphasised that while EU migrants had the right of residence in another EU country for up to three months, the country was under no obligation to pay social benefits during that period. If migrants stay for more than three months but less than five years, right of residence is dependent on whether they have sufficient resources to support themselves or their family members.

The eagerly anticipated ruling could be interpreted as a victory, or a blow, for Cameron. It is a victory in the sense that it supports the British government’s drive to curb benefit abuse by EU migrants. The British, Danish and Irish had voiced strong support for the German position in the dispute. But it is also a defeat in the sense that the ruling means that these curbs can be achieved within the existing rules.

Had the ruling gone the other way, it would have probably triggered a debate about reforming freedom of movement in Germany. Now, British calls for an overhaul of the existing systems – including the availability of benefits to EU migrants who are in employment – are less likely to be heard.

Manfred Weber, head of the centre-right European People’s party (EPP) group in the European parliament, said: “The ruling sends a very strong message because it highlights that member states have many options and legal tools at their disposal to make sure their social system is not being abused, without contesting freedom of movement, which is a basic principle of the European Union. It sends a clear signal to the member states and to the British prime minister in particular.”

The German government welcomed the ruling in a labour ministry statement: “The right to free movement of persons is a high principle and symbolises the idea behind the European Union. But there are limits to the right for equal treatment, which are defined in the European directive on free movement”.

But while some German politicians saw the ECJ’s ruling as outlining the limits of free movement within the European Union, others welcomed the court’s statement as a confirmation of the same principle: “The ruling reaffirmed the principle of free movement”, said Green party parliamentary leader Anton Hofreiter. “It also reaffirmed that people who are prepared to look for work across Europe have a right to draw social benefits”.

In Britain, the ruling was welcomed by members of both the government and the opposition.

Conservative MEP Timothy Kirkhope said that while the ruling addressed German legislation, it would have “wide-ranging implications for how the UK can tighten its welfare system to ensure only migrants that make a contribution can receive something back”.

“This court case and this ruling show quite clearly that the UK is not alone in its concerns about restoring free movement to its core principle: free movement of labour. The government will be heartened by this decision today,” he said.

Rachel Reeves, the shadow work and pensions secretary, said: “Labour welcomes today’s ruling on restricting benefits to immigrants from EU member states who travel simply to claim benefits. Labour has repeatedly called on the government to act to ensure that the UK benefit system is only there for those prepared to contribute, including extending the three month waiting time EU migrants have to wait before claiming benefits and ending the unfair practice of child benefit being sent abroad. It’s now time for ministers to act.”

The Liberal Democrat MEP Catherine Bearder said: “Today’s ruling shows that while EU free movement is non-negotiable, it is not an unfettered right. Liberal Democrats are clear that the freedom to live, work and study across the continent should not mean the freedom to claim. Safeguards can and should be put in place to prevent benefit tourism and abuse of the system.”