For an existing GUI webapp built upon a SpringMVC (3.0.1) stack, I want to expose a RESTful API. What would you consider a reasonable option?
- Integrate with another framework like Jersey (seems like restarting from scratch)?
- Use the built in SpringMVC integration of JAXB / Jackson?
- Should I just add another servlet-mapping
/api/*to the sameDispatchServlet? - All the views are coded in jsp. Should I use jsp templates for my XML API?
- Build a separate new webapp with common dependencies?
- Anything else?
Hereafter is my web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/web/*</url-pattern>
</servlet-mapping>
</web-app>
And my spring context descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<context:component-scan base-package="net.mycrub.poc.controllers" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/views/"
p:suffix=".jsp"
p:viewClass="org.springframework.web.servlet.view.JstlView" />
</beans>
Thanks in advance ; mapping examples are welcome 🙂
I will take a stab at a few of your questions…
Integration/Separate: I would recommend you build your REST application separate from your WEB GUI. A big reason why people build API’s is to expose it to multiple applications so I wouldn’t tie the WEB GUI so closely with the API. This thought is in line with the whole separation of concerns concept. For example if later on you build a Mobile app to access the same API it won’t be impacted if you need to take down your WEB GUI for a deployment.
Jackson/ JAXB: I would highly recommend Jackson. In my opinion is it has the best mix of performance and functionality.
Spring MVC/Jersey: I would recommend sticking with Spring for a couple reasons:
1.) Less of a learning curve, if you’re familiar with Spring already there is nothing wrong with sticking with it.
2.) Dependency injection in Jersey today is more or less broken (http://java.net/jira/browse/JERSEY-517). So if you’re used to how well this works in Spring like I was, switching to Jersey will be painful. This is supposed to be addressed in the 2.0 release but that’s not finalized yet.