title: Combining @PathVariable and @RequestBody
body: <p>I have a <code>DTO</code>:</p>
  <pre class="lang-java prettyprint-override"><code>public class UserDto {
    private Long id;
    private String name;
  }
  </code></pre>
  <p>and <code>Controller</code>:</p>
  <pre class="lang-java prettyprint-override"><code>@RestController
  @RequestMapping(&quot;user&quot;)
  public Class UserController {
    @PostMapping(value = &quot;{id}&quot;)
    public String update(@PathVariable String id, @RequestBody UserDto userDto){
      userDto.setId(id);
      service.update(userDto);
    }
  }
  </code></pre>
  <p>What I don't like is manually putting <code>ID</code> from <code>@PathVariable</code> to <code>DTO</code>: <code>userDto.setId(id);</code>.</p>
  <p>For POST request <code>/user/5</code> with body: <code>{ name: &quot;test&quot; }</code>, how could I automatically set <code>ID</code> in <code>DTO</code>, so that you'd get <code>DTO</code> like below?</p>
  <pre><code>{
    id: 5,
    name: &quot;test&quot;
  }
  </code></pre>
  <p>Basically, I'd like to have something like:</p>
  <pre class="lang-java prettyprint-override"><code>@RestController
  @RequestMapping(&quot;user&quot;)
  public Class UserController {
    @PostMapping(value = &quot;{id}&quot;)
    public String update(@RequestBody UserDto userDto){
      service.update(userDto);
    }
  }
  </code></pre>
  <p>Is there a way to accomplish this?</p>
  <p>Thank you! :)</p>
  <p>EDIT: this is an old question, still unanswered, so I'd like to add new perspective to this question.</p>
  <p>Another problem we had is validation, to specific - defining custom constraint that does validation based on some field and <code>id</code>.</p>
  <p>if we remove <code>id</code> from request body, then how can we access it from custom constraint? :)</p>
  <p>EDIT 2: <a href="https://stackoverflow.com/users/3389507/italktothewind">italktothewind</a> created issue on Github: <a href="https://github.com/spring-projects/spring-framework/issues/28637" rel="nofollow noreferrer">https://github.com/spring-projects/spring-framework/issues/28637</a></p>