Sping 学习笔记7—— SpringMVC的restful 服务

来源:互联网 发布:西安 人工智能 编辑:程序博客网 时间:2024/06/05 03:36

普通的restful服务示例

package com.test.controller.rbac;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controller@EnableWebMvc@RequestMapping("/rest")public class RbacAdminController {     @RequestMapping(value="/test1",method=RequestMethod.POST,produces={"application/json;charset=UTF-8"})     @ResponseBody     public String test1(String name,String password){         System.out.println("name="+name);         return "abc";     }}

web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <display-name>Archetype Created Web Application</display-name>  <servlet>    <servlet-name>dispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>dispatcherServlet</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping></web-app>  

dispatcherServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:beans="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!-- 自动扫描的包名 -->    <context:component-scan base-package="com.cn.ustcit.services"></context:component-scan>    <context:annotation-config />    <mvc:resources mapping="/assets/**" location="/assets/" />      <mvc:resources mapping="/js/**" location="/js/" />    <!-- 默认的注解映射的支持 -->    <mvc:annotation-driven ignoreDefaultModelOnRedirect="true" >    </mvc:annotation-driven>    <!-- 视图解释类 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/Views/" />        <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->        <property name="suffix" value=".jsp" />    </bean>    <!-- HandlerMapping -->      <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>      <!-- HandlerAdapter -->      <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/></beans>
0 0
原创粉丝点击