我正在尝试为我现有的struts项目实现spring RESTful服务,但是无法完成相同的.i添加了Spring rest服务但是当我尝试运行现有的应用程序时,它无法正常运行..我是完整的相同的代码.i具有下面定义的所有文件,请检查并提供相同的帮助。
下面是我的Web.xml文件
<!-- spring context config-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml; classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring listner-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/showAllgrants/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- listener>
<listener-class>
org.nea.web.struts.ContextListener</listener-class>
</listener-->
<!--servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>org.abc.efg.web.AppResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet-->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>Index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/NotFound.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/NotFound.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/Error.jsp</location>
</error-page>
下面是我的spring webmvc.xml
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--
Enables automatic mapping of fund objects to and from JSON
-->
<mvc:annotation-driven/>
<!--
Setup spring to pull in @Controller, @RequestMapping, etc Configuration scans specified packages
for classes configured as Spring managed beans and automatically sets up objects annotated with
@Controller, @Service etc.
-->
<context:component-scan base-package="org.web.struts.uniservs" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!--
Configures view for returning JSON to the client
-->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="text/plain"/>
</bean>
<!--
maps handler methods based on HTTP paths
-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="order" value="1" />
<property name="messageConverters">
<list>
<!-- Default converters -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/*">strutsWrappingController</prop>
</props>
</property>
</bean>
<bean id="strutsWrappingController" class="org.springframework.web.servlet.mvc.ServletWrappingController" scope="singleton">
<property name="servletClass">
<value>org.apache.struts.action.ActionServlet</value>
</property>
<property name="servletName">
<value>action</value>
</property>
<property name="initParameters">
<props>
<prop key="config">/WEB-INF/struts-config.xml</prop>
</props>
</property>
</bean>
<bean id="empService"
class="org.nea.spring.services.implementations.employee"/>
<bean id="iaUtilitiesService"
class="org.nea.spring.services.implementations"/>
</beans>
下面是我的struts TestAction.java
@Controller
public class TestAction extends ActionSupport implements ServletRequestAware, ServletContextAware {
private static final long serialVersionUID = 6274330790290034472L;
@RequestMapping(value="/showAllgrants/{year}/{status}/{stateId}",
method=RequestMethod.GET,headers="Accept=application/xml, application/json")
public String showAllgrants(@PathVariable String year,@PathVariable String status,@PathVariable String stateId) {
/*commented to test restful service
User user = (User) request.getSession().getAttribute("user");
if (user == null) {
return LOGIN;
}
*/
List<UniServGrantInfo> grantInfoList = new ArrayList<UniServGrantInfo>();
//String year = request.getParameter("year");
//String status = request.getParameter("status");
//String stateId = request.getParameter("stateId");
grantInfoList = uniservService.showAllgrants("ITSMAXK", year, status, stateId);
/* commented to test restful service
JSONObject jsonObject = new JSONObject(grantInfoList);
JSONWrapper jsonWrap = new JSONWrapper();
jsonWrap.setObjects(grantInfoList.toArray());
jsonObject = new JSONObject(jsonWrap);
String jsonString = convertJSONObjectToString(jsonObject);
try {
jsonObject = new JSONObject(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
inputStream = new ByteArrayInputStream(jsonString.toString().getBytes());
request.getSession().setAttribute("grantInfoList", grantInfoList);
request.getSession().setAttribute("year", year);
request.getSession().setAttribute("status", status);
request.getSession().setAttribute("stateId", stateId);
*/
return SUCCESS;
}
}
下面是我的struts-config.xml文件
<action name="showAllgrants"
class=""
method="showAllgrants">
<result type="stream">
<param name="contentType">application/json</param>
<param name="contentType">text/json</param>
<param name="inputName">inputStream</param>
</result>
</action>