[英]Spring RESTful web service using only XML config
I've been using only XML configuration to make MVC web applications (no annotation). 我一直只使用XML配置来制作MVC Web应用程序(没有注释)。
Now I want to make a RESTful web service with Spring but I could not find any tutorial that doesn't use annotation. 现在,我想用Spring创建一个RESTful Web服务,但是找不到任何不使用注释的教程。
Is there a way to build a RESTful web service with only XML configuration ? 有没有一种方法可以仅使用XML配置来构建RESTful Web服务?
Or do I HAVE TO use annotation ? 还是我必须使用注释?
For example, you can deploy an MVC pattern web application using only XML configuration like below. 例如,您可以仅使用如下所示的XML配置来部署MVC模式Web应用程序。
<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. 但是,当我尝试为URL映射方法(例如HTTP方法
POST
,URL: /student/1/Adam
时,我碰壁了,以便可以添加一个学生。
The URL format would be like this: /[resource]/[id]/[name]
URL格式如下:
/[resource]/[id]/[name]
I could map /student/1/Adam
to a controller by putting a pattern in the entry key like: 我可以通过在输入键中放置一个模式来将
/student/1/Adam
映射到控制器,例如:
<entry key="/student/regex-to-allow-number/regex-to-allow-string">
But how should I parse the URI within my controller ? 但是我应该如何解析控制器中的URI?
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. 我可以使用
String.split()
或类似的方法来解析URI,但我想知道是否还没有解决方案,以便避免重新发明轮子。
<?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. 这是rest-servlet.xml。 This file must be configure in web.xml by using DispatcherServlet class
必须使用DispatcherServlet类在web.xml中配置此文件
<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 上面的代码写在web.xml的启动时加载1中,给出了spring-security.xml和spring-config.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.