In his first Commons speech since returning as an MP, Mr Johnson said he believed David Cameron would be successful in his EU negotiations.
But the London mayor said the UK had to be willing to "strike out" and forge "an alternative future" if the desired result could not be secured.
The government has promised to hold an in/out referendum on the EU by 2017.
Mr Johnson's comments come after European Commission President Jean-Claude Juncker suggested David Cameron wanted to use the EU vote to "dock" the UK permanently into the 28-nation bloc.
Mr Juncker told a German newspaper the question of a British exit from the EU "does not arise", as this was not what the UK was seeking.
BBC deputy political editor James Landale said it showed what pressure the prime minister will come under over Europe, from both Brussels and his own backbenchers.
Mr Cameron spent the last week of May on a whirlwind tour of Europe to lobby leaders over his proposed EU reforms, which include tougher rules to prevent migrants claiming benefits.
Addressing the House of Commons, Mr Johnson, the new MP for Uxbridge and Ruislip, congratulated Mr Cameron on his "schmoozathon" around Europe.
He said the all-Conservative government had "a clear mandate" to deliver change in the EU.
"And we can - we can win that argument by being relentlessly positive and by making it clear that what we are advocating is not simply in the interests of Britain, but in the interest of the entire European Union," Mr Johnson said.
But Mr Johnson added: "If you are going to go into a difficult international negotiation of this kind, then you have got to be prepared to walk away if you don't get the result that you want.
"If we don't get the deal that is either in the interests of this country or of Europe, then we should be prepared to strike out and forge an alternative future that could be just as glorious and just as prosperous with a free trading arrangement."
Meanwhile, at a foreign affairs conference in London at Chatham House, Foreign Secretary Philip Hammond said that ahead of the referendum the government would be "selling hard" the advantages of staying in the EU, if a good package of reforms had been secured.
"The prime minister's made no secret of the fact that what he wants to do in Europe is negotiate a reform of the European Union and then settle this question once and for all that Britain is an active leading member of the EU of a reformed EU, going forward," he said.
The government has introduced legislation paving the way for a vote on the EU by the end of 2017 at the very latest, although there is speculation it could take place as early as next autumn.
The eligibility rules for the poll will be broadly the same as for a general election, meaning citizens from most EU countries living in the UK will not get a vote.
Irish citizens in the UK qualify, as do residents from Malta and Cyprus along with others from the Commonwealth.
The government has ruled out extending the franchise to 16 and 17-year-olds.

The 25-year-old midfielder signed a two-year deal in 2015 after leaving Derby County.
"It's been a long time coming but it's signed and sealed," he said.
"I'm delighted. I just want to play as much as I can and try to get this club as high as they can as well. Hopefully we will stay in the league."
The Brewers are 18th in the table, four points above the relegation zone.
The UK must be prepared to walk away from the EU if sufficient reform is not achieved, Boris Johnson has said.
[NEXT_CONCEPT]
Midfielder Tom Naylor has signed a one-year extension to his Burton Albion contract which will keep him at the Championship side until June 2018.