Reputation: 2848
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?
/api/*
to the same DispatchServlet
? 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 :-)
Upvotes: 4
Views: 1352
Reputation: 758
This question is several years old, anyway a reasonable approach today would be migrating the Spring webapp to Spring Boot including the Spring Data REST module. All the JPA repositories would be automagically exported as a comprehensive and discoverable REST API.
Upvotes: 1
Reputation: 777
It depends on several factors. The first question I would ask is regarding the performance. If you have just one webapp, either the GUI users or the WS users can overload the server. So, maybe a better solution is to have 3 different artifacts.
GUI WebApp WS WebApp Business Common Artifact
And you can escalate each webapp according your needs.
Hope it helps.
Upvotes: 1
Reputation: 502
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.
Upvotes: 3