Art Scroll Kesuvos 9a4 note 27 says the Chida says that he violated her, others say that while she consented, she was a minor at the time and it was a violation in the legal sense (Sanhedrin 69b). See also Yevamos 61b; note 7 on Kesuvos 9a2; Rav Yaakov Emden in Mitpachos Sefarim 68; Atzei Aruzim, Even haezer 11:12
Art Scroll Kesuvos 9b1 brings the second answer that Uriah had (like the other soldiers in the army) had given her a divorce before going to war. Note 1 goes into details about this and discusses the machlokes between Rashi and Tosfos about whether it was a conditional divorce or not. Tosfos cites Rabbeinu Tam that it was unconditional and that since it was expected that she would remain faithful and remarry him when he returned, King David was castigated for abuse of authority for cohabiting with her.
In either case, since it was a violation of halacha, she would not have been punished for disobeying authority as a king cannot (as an example) command someone to eat non-kosher food. This is like a parent asking a child to violate Shabbos because of the fifth commandment כבוד אב ואם
דברי הרב ודברי התלמיד דברי מי שומעים