Skip to content
Daniel Gradecak edited this page Oct 8, 2018 · 1 revision

This small library enables the usage of Spring @MVC within Alfresco. Instead of writing webscripts and all the glue configuration that goes with that, you can simply write Springframework Controllers, Services/Components, ... with Spring annotations.

Alfresco @MVC REST is mainly used on the Alfresco repository side, but is also suitable for Alfresco Share.

@Controller
@RequestMapping("/document")
public class DocumentController {

	@Autowired
	private SomeService service;

	@RequestMapping(value = "{id}", method = { RequestMethod.GET })
	@ResponseBody
	public ResponseEntity<?> index(@PathVariable Long id) {
	  // yes this works in Alfresco
	  return new ResponseEntity<?>(service.get(id), HttpStatus.OK);
	}
}

This library offers some out of the box configurations for webscript descriptor bindings. The entry endpoint is by default /alfresco/service/mvc only if the DispatcherWebscript is configured with bean names as follows: "webscript.alfresco-mvc.mvc.post", "webscript.alfresco-mvc.mvc.get", "webscript.alfresco-mvc.mvc.delete", "webscript.alfresco-mvc.mvc.put"

otherwise you are free to create your own webscript descriptors and configure the beans correctly.

A DispatcherWebscript can be connfigured :

with @Enabled annotations

@Configuration
@EnableAlfrescoMvcAop(basePackageClasses = QueryTemplateService.class)
@EnableAlfrescoMvcRest({ @AlfrescoDispatcherWebscript(servletContext = AlfrescoMvcQueryTemplateServletContext.class) })
public class AlfrescoMvcQueryTemplateModuleConfig {

}

@Configuration
@ComponentScan(basePackageClasses = { AlfrescoMvcQueryTemplateController.class })
@EnableAlfrescoMvcServlet
// use @EnableWebMvc if the default config should not be used
public class AlfrescoMvcQueryTemplateServletContext {

}

with Java config

  @Bean(name = { "webscript.alfresco-mvc.mvc.post", "webscript.alfresco-mvc.mvc.get", "webscript.alfresco-mvc.mvc.delete", "webscript.alfresco-mvc.mvc.put" })
  public DispatcherWebscript dispatcherWebscript() {
    DispatcherWebscript dispatcherWebscript = new DispatcherWebscript();
    dispatcherWebscript.setContextClass(org.springframework.web.context.support.AnnotationConfigWebApplicationContext.class);
    dispatcherWebscript.setContextConfigLocation(AlfrescoMvcHateoasConfig.class.getName());
    return dispatcherWebscript;
  }

AlfrescoMvcHateoasConfig has to be a spring' @Configuration class and please note the contextClass property should be AnnotationConfigWebApplicationContext

  @Configuration
  @ComponentScan(basePackageClasses = { "...controller" })
  @EnableWebMvc
  public class AlfrescoMvcHateoasConfig {
    ...
  }

with a servlet-conext.xml file

  <bean id="webscript.alfresco-mvc.mvc.post" class="com.gradecak.alfresco.mvc.webscript.DispatcherWebscript" parent="webscript">
    <property name="contextConfigLocation" value="classpath:alfresco/module/YOUR_MODULE/context/servlet-context.xml" />
  </bean>
  
  <alias name="webscript.alfresco-mvc.mvc.post" alias="webscript.alfresco-mvc.mvc.get" />

Alfresco @MVC

Clone this wiki locally