简体   繁体   中英

RESTful API for a SpringMVC app

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?

  1. Integrate with another framework like Jersey (seems like restarting from scratch)?
  2. Use the built in SpringMVC integration of JAXB / Jackson?
  3. Should I just add another servlet-mapping /api/* to the same DispatchServlet ?
  4. All the views are coded in jsp. Should I use jsp templates for my XML API?
  5. Build a separate new webapp with common dependencies?
  6. 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM