Rotary joint for rotating a tool, in particular a sensor for scanning test objects to generate measuring signals from a coordinate measuring device, wherein - the rotary joint has a part which can be rotated about an axis of rotation and to which the tool is or can be connected, - the rotary joint has a stationary part, relative to which the rotatable part can be rotated about the axis of rotation, - the rotatable part is movable relative to the stationary part in the axial direction of the axis of rotation, wherein, in a first axial position, rotation of the rotatable part about the axis of rotation is prevented, and wherein, in a second axial position, rotation of the rotatable part about the axis of rotation is possible, - the rotary joint has a first magnetic device (13, 17), which is configured and arranged in such a way that a magnetic force generated by the first magnetic device (13, 17) is exerted on the rotatable part in order to keep the rotatable part in the first and/or second axial position and/or to effect a movement of the rotatable part from the first to the second axial position and/or vice versa, - the rotary joint has at least one buffer region (101, 103) of a magnetostrictive material, which is arranged on the rotatable part, on the stationary part and/or a stop element having a stop for reaching the second axial position, in such a way that a length of the buffer region (101, 103) in the axial direction can be modified by a magnetic field of the first magnetic device (13, 17) and/or a magnetic field of another magnetic device, so that the rotatable part comes to a stop at the buffer region (101, 103) when reaching the first or second axial position. - the rotary joint has a part which can be rotated about an axis of rotation and to which the tool is or can be connected, - the rotary joint has a stationary part, relative to which the rotatable part can be rotated about the axis of rotation, - the rotatable part is movable relative to the stationary part in the axial direction of the axis of rotation, wherein, in a first axial position, rotation of the rotatable part about the axis of rotation is prevented, and wherein, in a second axial position, rotation of the rotatable part about the axis of rotation is possible, - the rotary joint has a first magnetic device (13, 17), which is configured and arranged in such a way that a magnetic force generated by the first magnetic device (13, 17) is exerted on the rotatable part in order to keep the rotatable part in the first and/or second axial position and/or to effect a movement of the rotatable part from the first to the second axial position and/or vice versa, - the rotary joint has at least one buffer region (101, 103) of a magnetostrictive material, which is arranged on the rotatable part, on the stationary part and/or a stop element having a stop for reaching the second axial position, in such a way that a length of the buffer region (101, 103) in the axial direction can be modified by a magnetic field of the first magnetic device (13, 17) and/or a magnetic field of another magnetic device, so that the rotatable part comes to a stop at the buffer region (101, 103) when reaching the first or second axial position.