仅使用XML配置的Spring RESTful Web服务 [英] Spring RESTful web service using only XML config
问题描述
我一直只使用XML配置来制作MVC Web应用程序。(没有注释)
I've been using only XML configuration to make MVC web applications.( no annotation)
现在我想用Spring创建一个RESTful Web服务但是我可以找不到任何不使用注释的教程。
Now I want to make a RESTful web service with Spring but I could not find any tutorial that doesn't use annotation.
有没有办法构建只有XML配置的RESTful Web服务?
或者做我必须使用注释吗?
Is there a way to build a RESTful web service with only XML configuration ?
Or do I HAVE TO use annotation ?
例如,您可以仅使用如下所示的XML配置部署MVC模式Web应用程序。
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>
但是,当我尝试为URL映射方法时,我遇到了障碍,例如
HTTP方法:POST,URL:/ student / 1 / Adam - 这样我就可以添加学生。
URL格式如下。 / [资源] / [id] / [名称]
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]
我可以通过在条目键中添加模式来将/ student / 1 / Adam映射到控制器,如
但是我应该如何解析控制器中的URI?
I could map /student/1/Adam to a controller by putting a pattern in the entry key like But how should I parse the URI within my controller ?
我可以通过使用String.split()或类似的东西来解析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.
推荐答案
<?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>
这是rest-servlet.xml。必须使用DispatcherServlet类在web.xml中配置此文件
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>
以上代码在网上写。 xml
load-on-startup 1给出spring-security.xml和spring-config.xml
Above code write in web.xml load-on-startup 1 give for spring-security.xml and spring-config.xml
这篇关于仅使用XML配置的Spring RESTful Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!