The suspect, identified only as 29-year-old Abshir A, was found guilty of membership of a foreign terrorist group.
The court said the accused, who was born in Mogadishu, left Germany for Somalia in 2012 after becoming radicalized and was active for the militant group until early 2014.
He spent around four months undergoing combat training upon arrival, during which time Shabaab militia taught him how to handle weapons and employ guerrilla tactics, according to a court statement.
The accused was then sent to a Shabaab base but left “shortly afterwards because of health problems”, it added.
He remained in Somalia however, and only returned last year.
He was arrested upon his return at Frankfurt airport in July 2016.
The accused denied taking part in any fighting in Somalia, where the Shabaab are seeking to overthrow the country's internationally-backed government.
The group has also carried out deadly attacks elsewhere in East Africa.
German courts have jailed a number of returning jihadists for their membership in foreign terror groups.
Five men were sentenced to prison terms of up to five years in 2016 for having joined the Shabaab in Somalia.
In another case last year, a court jailed three young German men for up to four-and-a-half years for having joined extremist fighters in Syria in 2013.