New Zealand claimed third place at the Rugby World Cup with a 40-17 win over Wales on Friday night.

The All Blacks were looking to win the tournament for the third time running and fourth overall this year in Japan, only to run into an inspired England in last weekend's semifinals.

But New Zealand did manage to end the competition on a winning note in the bronze-medal match at Tokyo Stadium, scoring four first-half tries and two after the break to see off Wales, which lost its semifinal to South Africa.

The match marked the end for both team's head coaches, with New Zealand's Steve Hansen stepping down after eight years in charge of his side and Wales' Warren Gatland departing after 12 years at the helm of his.

"I'd like to say how proud I am of our team," said Hansen. "There was a lot of external talk about not wanting to play this game. We've come off an extremely disappointing result against England, where we got beaten by a team who played better than us on the day. But I think we've come back out and shown some real character and commitment to the jersey.

"I'd like to commend Wales as well, because they came to play and it was a good game of footy. At the end of the day, the game is bigger than all of us, and we're continually trying to capture people to become part of this great game. If you come with intent to play, and lots of tries were scored today by both teams, we can capture more and more people and get them excited about the game."

New Zealand scored early tries through Joe Moody and Beauden Barrett before Wales hit back through Hallam Amos, but a double from Ben Smith shortly before halftime gave the Welsh a mountain to climb in the second half.

"In fairness to the All Blacks, I thought they played exceptionally well," said Gatland. "They were outstanding on attack. Conceding the try on halftime was disappointing, I thought 21-10 would have been not bad after they started so well. Then we got back into the game and there probably wasn't a lot of difference between the two sides in the second half.

"It was obvious to me just watching the first half that the five-day turnaround after we played South Africa, and losing four players in that game, some players definitely struggled with the game being so quick in terms of the turnaround. I thought the bench gave us some impetus when they came on. But no complaints about the results. I thought the All Blacks were great and they deserved to win."

Richie Mo'unga missed an early opportunity to put New Zealand in front when he kicked a penalty against the post in the fourth minute, but the All Blacks got themselves on the scoreboard a minute later with the opening try. Brodie Retallick caused havoc in the Welsh defense with a rampaging run through the middle, and Moody was on hand to receive the pass and charge toward the line. This time, Mo'unga made no mistake with the conversion.

Barrett scored New Zealand's second in the 13th minute, taking a clever reverse pass from Aaron Smith before streaking over the line. Mo'unga again converted.

Wales refused to be disheartened, however, and hit back when Amos found a gap in the All Blacks' defense in the 19th minute after a sustained period of Welsh pressure. Rhys Patchell converted Amos' try and then added a penalty minutes later to close the gap to four points.

Ben Smith put New Zealand back in control with two tries in the closing 10 minutes of the first half. First he rumbled through a series of attempted tackles to cross the line in the 33rd minute, before racing down the wing after the halftime gong had sounded to send the All Blacks into the break with a healthy lead.

Ryan Crotty put the All Blacks further ahead with a try two minutes after the restart, before a video replay denied Ben Smith his hat trick after he had dived over in the corner.

Josh Adams pulled one back for Wales in the 60th minute, extending his lead at the top of the try-scorer's overall table to seven.

But Mo'unga made sure New Zealand had the final say, skipping through a gap in the Welsh defense to touch down in the 76th minute.

"If you're going to play in the final that you don't want to be in, you want to win it," said Hansen. "We're a competitive bunch and we're proud bunch. All week we talked about the need to represent the legacy of the jersey. What would it expect of us as a group and what would our fans expect? And it's those expectations that help you get up and show your true character."