How to create a new version of Confluence page via Java API?

I already asked this in the Atlassian Community and it was recommended to ask the developer community: I am trying to manipulate a Confluence page using the Java API. Changing the content of the page works as expected but the change ist not correctly reflected in the page history - the content is overwritten with the new version the version count is increased but the old version is simply lost.

That’s my code (I’m using Adaptavist ScriptRunner so that’s Groovy code but the API-calls should all be the same in Java):

import com.atlassian.sal.api.component.ComponentLocator

import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.core.Modification

import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.jsoup.nodes.Document

PageManager pageman = ComponentLocator.getComponent(PageManager)

def page = pageman.getPage("scriptrunner", "Testpage for Update")
def src=page.getBodyAsString()

def doc = Jsoup.parse(src)
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml);

def element = doc.getElementsContainingText("Content")

if (element.size()>0) {
  element[0].text("Updated Content")
  pageman.saveNewVersion(page, new Modification<Page>() {
    public void modify(Page p) {
      p.version = p.version + 1
      p.bodyAsString = doc.body().toString()
    }
  }, DefaultSaveContext.MINOR_EDIT);
}
else {
  log.warn("no content found")
}

For saving the page I tried:

pageman.saveContentEntity(page, DefaultSaveContext.MINOR_EDIT)

leading to the same result - page content changed correctly history lost.

Can someone give me a hint how to produce a correct page history?

Thanks
Kurt

I found a solution myself and posted it as a reply to my community question here:

My initial problem of modifying content with scriptrunner is now also solved with this new article: