First some annotated Java code:
@Component
@Controller()
public class SampleController {
@Autowired
private SampleService service;
@RequestMapping(value = "/samples/{reference}", method = {RequestMethod.GET})
@ResponseBody
public Sample getSampleByReference(@PathVariable String reference) {
return service.getSampleByReference(reference);
}
}
Then some Spring config:
<beans>
<aop:aspectj-autoproxy />
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And that's about it, folks. Spring will automagically handle JSON and XML accept reponses depending on what's in your classpath.
A fully working sample can be found here. And of course you can then generate REST documentation using RESTdoclet. :) Build a Java web service has never been easier in my opinion.
<beans>
<context:component-scan base-package="com.blah.sample" />
<aop:aspectj-autoproxy />
</beans>
Then some web.xml config in your web application:
<web-app> <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And that's about it, folks. Spring will automagically handle JSON and XML accept reponses depending on what's in your classpath.
A fully working sample can be found here. And of course you can then generate REST documentation using RESTdoclet. :) Build a Java web service has never been easier in my opinion.