After over 15,000 battles won in this game, from GE to GVG to raids (perhaps 1/3 from raids), I know a few things now to pass along. Never ever defend your city with two spearfighters or some similar foolish lineup. Most raiders see this as a player not even trying and it makes them want to attack even more. This is true. Put your best defense up that you have, always. Even if you know it has no chance to win. Raiders hate taking damage on raids...even worse some losses.
Secondly, as stated above, there is one full proof defensive method...collect on time every time. If your city never has any items to raid, this often deters the attacker eventually and causes them to skip over you. Plus there is no loss for you at all if they collect nothing, your troops regenerate automatically, so no harm if you have collected.
Lastly, this is a game built to accommodate many playing styles, including raiding. There are GB’s and quests which reward raiders in this game. Perhaps that is not your style, but it is the game style of some and their style is no better or worse than your own, it’s just different. Understand this and accept it, do not send nasty messages to someone who has raided you, it will only make matters worse in most cases. Learn to play the game such that you realize all of the playing styles and understand you will encounter them all throughout your game time. Never take it personal, it’s just a game. When someone gets mad at a raider for attacking and plundering they are really mad that the game has this feature built into it. As long as it is a part of the game, there will be raiders just like there are farmers etc.. Collect on time and pay no attention to it if it bothers you. This game was built and designed for all types of players and you choose yourself whether to be an able farmer, attacker or both.