Ireland bounced back from a record loss against England and boosted their World Cup preparations by inflicting a rare home defeat on Wales.
Ireland's 22-17 victory meant that Wales were beaten in Cardiff for the first time since November 2017.
Wing Jacob Stockdale led the way with two first-half tries as Wales head coach Warren Gatland's final Principality Stadium Test before he departs after the World Cup ended frustratingly.
Ireland, crushed 57-15 by England seven days ago, bossed most critical areas ahead of next Saturday's return game in Dublin.
Fly-half Jack Carty added a conversion and penalty for the visitors, while his opposite number Jarrod Evans kicked a penalty before going off at half-time and being replaced by Rhys Patchell, with a second-half penalty try sealing Wales' fate.
Wales finished strongly, with debutant wing Owen Lane and Patchell claiming late tries and Patchell converting both, but it was not enough.
Gatland names his 31-man World Cup squad on Sunday, yet only Lane and Patchell made strong claims for any of the few remaining places.
Ireland boss Joe Schmidt, though, will have been encouraged by his team's recovery powers following a shambolic effort at Twickenham when England crushed them under an eight-try onslaught.
Stockdale, who departed at the interval, Carty, centre Bundee Aki, skipper Peter O'Mahony and his fellow flanker Tadhg Beirne were among those that impressed.