A man who spat at police officers and told them he was HIV positive has avoided jail.