java 仅使用 XML 配置的 Spring RESTful Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29911137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring RESTful web service using only XML config
提问by Ascendant
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 ?
Or do I HAVE TO use annotation ?
有没有办法只用 XML 配置来构建 RESTful Web 服务?
还是我必须使用注释?
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.
The URL format would be like this: /[resource]/[id]/[name]
但是,当我尝试映射 URL 的方法(例如 HTTP 方法 : POST
、URL : /student/1/Adam
- 以便我可以添加学生时,我碰壁了。
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 ,但我想知道是否已经有一些解决方案,这样我就可以避免重新发明轮子。
回答by Divyang Upadhyay
<?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
这是rest-servlet.xml。该文件必须使用 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 load-on-startup 1 给 spring-security.xml 和 spring-config.xml