Spring RESTful web service using only XML config

2019-05-24 08:00发布

问题:

I've been using only XML configuration to make MVC web applications (no annotation).

Now I want to make a RESTful web service with Spring but I could not find any tutorial that doesn't use annotation.

Is there a way to build a RESTful web service with only XML configuration ?
Or do I HAVE TO use annotation ?

For example, you can deploy an MVC pattern web application using only XML configuration like below.

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

   <bean class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" id="springParameterMethodNameResolver">
    <property name="paramName" value="action"/>
   </bean>

   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
       <map>
            <entry key="/test.do" >
                <ref bean="testController"  />
            </entry>
            <entry key="/rest/test">
                <ref bean="testRESTController"/>
            </entry>
        </map>

    </property>
   </bean>

   <!-- My Beans -->
   <bean id="testMethodNameResolver" class="com.rhcloud.riennestmauvais.spring.test.TestMethodNameResolver">
   </bean>

   <!-- Test -->
   <bean class="com.rhcloud.riennestmauvais.spring.test.TestController" id="testController">
        <property name="delegate" ref="testDelegate"/>
        <property name="methodNameResolver" ref="testMethodNameResolver"></property>
        <!-- <property name="methodNameResolver" ref="springParameterMethodNameResolver"></property> -->
   </bean>
   <bean class="com.rhcloud.riennestmauvais.spring.test.TestDelegate" id="testDelegate">
   </bean>

However, I hit a wall when I was trying to map a method for URL for example HTTP method : POST, URL : /student/1/Adam - so that I could add a student.
The URL format would be like this: /[resource]/[id]/[name]

I could map /student/1/Adam to a controller by putting a pattern in the entry key like:

<entry key="/student/regex-to-allow-number/regex-to-allow-string">

But how should I parse the URI within my controller ?

I could parse the URI by using String.split() or something like that but I'm wondering if there isn't already some solution to this so that I could avoid reinventing the wheel.

回答1:

<?xml version="1.0" encoding="UTF-8"?>

	<beans xmlns="http://www.springframework.org/schema/beans"
	 xmlns:context="http://www.springframework.org/schema/context"
	 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xmlns:p="http://www.springframework.org/schema/p"
	 xsi:schemaLocation="
		http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        
 		<context:component-scan base-package="com.apmc.rest" />
 		<mvc:annotation-driven />
 		
    </beans>

This is rest-servlet.xml. This file must be configure in web.xml by using DispatcherServlet class

<servlet>
 	<servlet-name>rest</servlet-name>
 	<servlet-class>
  		org.springframework.web.servlet.DispatcherServlet
 	</servlet-class>
 	<load-on-startup>2</load-on-startup>
 </servlet>

 <servlet-mapping>
 	<servlet-name>rest</servlet-name>
 	<url-pattern>/rest/*</url-pattern>
 </servlet-mapping>

Above code write in web.xml load-on-startup 1 give for spring-security.xml and spring-config.xml