title: @RequestParam with Optional Spring REST
body: <p>I have a Spring REST endpoint with Optional request params like below: </p>
  
  <pre><code>public ResponseEntity&lt;Product&gt; globalProduct(@RequestParam Optional&lt;Integer&gt; id, @RequestParam Optional&lt;String&gt; name){
  
  return ResponseEntity.ok(tm));
  }
  </code></pre>
  
  <p>when I'm trying to test this endpoint which uses Mockito framework</p>
  
  <pre><code>@Test                                                                               
  public void testGlobalProduct() throws Exception {                        
      URI uri = UriComponentsBuilder.fromPath("/api/products")                  
              .queryParam("id", Optional.of(1)                                               
              .queryParam("name", Optional.empty())                                 
              .build().toUri();                                                       
      mockMvc.perform( MockMvcRequestBuilders.get(uri)                                
              .accept(MediaType.APPLICATION_JSON))                                    
              .andExpect(status().isOk())                                             
              .andExpect(content().contentType(MediaType.APPLICATION_JSON))           
              .andDo(MockMvcResultHandlers.print())                                   
              .andReturn();                                                           
  } 
  </code></pre>
  
  <p>So the queryParams id and name should be Optional.of(1) and Optional.empty() , but when I debug  in the rest api implementaion I see <code>id</code> and <code>name</code> has been wrapped with values <code>Optional[Optional[1]]</code> and <code>Optiona[Optional.empty]]</code> . </p>
  
  <p>I know I can use @RequestParams(required=false) but I dont want to use that way in this context. </p>
  
  <p>How do we unwrap <code>Optional[Optional[1]]</code> <code>Optiona[Optional.empty]]</code> Is this correct? Any suggestions please?</p>
  
  <p>Thanks in Advance!</p>