WidgetController { @RequestMapping( method = POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<WidgetConfigResource> createWidget( @PathVariable String slug, @RequestBody WidgetConfigResource widgetConfigResource) { Dashboard dashboard = dashboardService.load(slug); WidgetConfigResourceAssembler assembler = new WidgetConfigResourceAssembler(slug, dashboard.getId()); WidgetConfig createdWidgetConfig = widgetService.saveWidget(assembler.toEntity(widgetConfigResource)); WidgetConfigResource resource = assembler.toResource(createdWidgetConfig); return new ResponseEntity<>(resource, OK); } @Autowired  WidgetController(
      WidgetConfigService widgetService,
      WidgetConfigRepository widgetConfigRepository,
      DashboardService dashboardService); @RequestMapping(value = "/{widgetId}", method = GET, produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<WidgetConfigResource> getWidget(
      @PathVariable String slug, @PathVariable String widgetId); @RequestMapping( value = "/{widgetId}", method = DELETE, produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<Void> deleteWidget(@PathVariable String widgetId); @RequestMapping( method = POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<WidgetConfigResource> createWidget(
      @PathVariable String slug, @RequestBody WidgetConfigResource widgetConfigResource); @RequestMapping( value = "/{widgetId}/data", method = GET, produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<SourceDataResource> getData(
      @PathVariable String slug, @PathVariable String widgetId); @SuppressWarnings("unchecked") @RequestMapping(method = GET, produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<PagedResources<WidgetConfigResource>> getWidgets(
      @PathVariable String slug,
      @PageableDefault Pageable pageable,
      PagedResourcesAssembler pagedResourcesAssembler); @RequestMapping(value = "/all", method = GET, produces = MediaType.APPLICATION_JSON_VALUE) Resources<WidgetConfigResource> getAllWidgets(@PathVariable String slug);  }
@Test public void createWidget() throws Exception { ConstrainedFields fields = fields(WidgetConfigResource.class); WidgetConfig widgetConfig = widgetConfig(); WidgetConfigResource widgetConfigResource = widgetConfigResource(); when(dashboardService.load(any())).thenReturn(dashboard()); when(widgetService.saveWidget(any(WidgetConfig.class))).thenReturn(widgetConfig); MvcResult result = mvc() .perform( post("/api/dashboards/test/widgets") .contentType("application/json") .content(toJsonWithoutLinks(widgetConfigResource))) .andExpect(status().isOk()) .andExpect(containsResource(WidgetConfigResource.class)) .andDo( document( "widgets/create", requestFields( fields.withPath("title").description("The display title of the widget."), fields.withPath("type").description("The type of the widget."), fields .withPath("lastModified") .description( "The date the widget's configuration has last been modified."), fields .withPath("sourceConfigs") .description( "List of configurations for the data sources of this widget.")))) .andReturn(); WidgetConfigResource resource = fromJson(result.getResponse().getContentAsString(), WidgetConfigResource.class); assertThat(resource.getTitle()).isEqualTo("Production"); }