How to make your minecraft not spawn hostile mobs but spawning friendly mobs and still get hungry?

With that gamerule they would not despawn, but no new ones would spawn. You could set up a command block to kill all hostile mobs as soon as they spawn, but that would drop their loot. You could also kill that loot, but that would make every string or water bottle that you drop in the ground vanish. So I suggest teleporting the mobs down into the void instead.

Put a repeating command block chain near to where you first spawned in the world (can be down in the bedrock) and put these commands in them:

/tp @e[type=elder_guardian] ~ -65 ~
/tp @e[type=wither_skeleton] ~ -65 ~
/tp @e[type=stray] ~ -65 ~
/tp @e[type=husk] ~ -65 ~
/tp @e[type=zombie_villager] ~ -65 ~
/tp @e[type=evocation_illager] ~ -65 ~
/tp @e[type=vindication_illager] ~ -65 ~
/tp @e[type=creeper] ~ -65 ~
/tp @e[type=skeleton] ~ -65 ~
/tp @e[type=spider] ~ -65 ~
/tp @e[type=zombie] ~ -65 ~
/tp @e[type=slime] ~ -65 ~
/tp @e[type=ghast] ~ -65 ~
/tp @e[type=zombie_pigman] ~ -65 ~
/tp @e[type=enderman] ~ -65 ~
/tp @e[type=cave_spider] ~ -65 ~
/tp @e[type=silverfish] ~ -65 ~
/tp @e[type=blaze] ~ -65 ~
/tp @e[type=magma_cube] ~ -65 ~
/tp @e[type=ender_dragon] ~ -65 ~
/tp @e[type=wither] ~ -65 ~
/tp @e[type=witch] ~ -65 ~
/tp @e[type=endermite] ~ -65 ~
/tp @e[type=guardian] ~ -65 ~
/tp @e[type=shulker] ~ -65 ~
/tp @e[type=drowned] ~ -65 ~
/tp @e[type=phantom] ~ -65 ~
/tp @e[type=pillager] ~ -65 ~
/tp @e[type=ravager] ~ -65 ~

If you prefer just one command for this, use this one:

/tp @e[type=!item,type=!xp_orb,type=!area_effect_cloud,type=!leash_knot,type=!painting,type=!item_frame,type=!armor_stand,type=!ender_crystal,type=!egg,type=!arrow,type=!snowball,type=!small_fireball,type=!ender_pearl,type=!eye_of_ender_signal,type=!potion,type=!xp_bottle,type=!fireworks_rocket,type=!spectral_arrow,type=!llama_spit,type=!tnt,type=!falling_block,type=!boat,type=!minecart,type=!chest_minecart,type=!furnace_minecart,type=!tnt_minecart,type=!hopper_minecart,type=!skeleton_horse,type=!donkey,type=!mule,type=!bat,type=!pig,type=!sheep,type=!cow,type=!chicken,type=!squid,type=!wolf,type=!mooshroom,type=!snowman,type=!ocelot,type=!villager_golem,type=!horse,type=!rabbit,type=!polar_bear,type=!llama,type=!parrot,type=!villager,type=!player,type=!lightning_bolt,type=!dolphin,type=!cod,type=!salmon,type=!tropical_fish,type=!pufferfish,type=!turtle,type=!cat,type=!fox,type=!panda,type=!trader_llama,type=!wandering_trader] 0 -65 0

But careful: This command is made in 1.14.0, any entity that gets added after that needs adjustments to the command or else it gets killed.