您的位置:首页 > 编程语言 > Java开发

SpringMVC实现Restful风格的WebService

2016-04-02 18:13 375 查看

1.环境

JDK7

MyEclipse2014

tomcat8

maven 3.3.3

spring4.1.4

2.创建maven工程

  使用MyEclipse创建maven工程的方式可以参考这篇博文(链接1), 该博文中的第四小结是关于如何创建SpringMVC+maven教程。下面只给出创建好的目录结构,其中的部分文件如java程序是后面要完成的。

  

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/transport-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>transport</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>transport</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>


View Code

5.配置spring的配置文件(spring-servlet.xml)

  配置该文件时,约束文档要指定成spring4的约束,因为spring4实现了对restful的支持。

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 
<mvc:annotation-driven />
<context:component-scan base-package="com.dy.xidian" />
</beans>


6.controller的实现

  前面是已经把准备工作做完了,下面进入编程阶段。关于Restful风格的webService相信大家已经做过功课了,这里不在累述。因为应答是json格式,所以我们需要进行下面的工作。

6.1建立bean类

  在开始的图片中,我们可以看到有个Greeting.java类,它其实就是一个bean类,具体代码如下。

package com.dy.xidian;

public class Greeting {
private final long id;
private final String content;

public Greeting(long id, String content) {
this.id = id;
this.content = content;
}

public long getId() {
return id;
}

public String getContent() {
return content;
}
}


6.2 在控制器中处理请求

  SpringMVC会将请求交给一个控制器来处理,所以我们必须实现这样一个控制器,不过这个控制器在编写时要遵循restful风格。

  GreetingController.java

package com.dy.xidian;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@RequestMapping("/getuser")
public Greeting greeting(
@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template,
name));
}
}


虽然控制器返回的是一个Greeting对象,但是Spring会通过Jackson工具将其转换为一个json串。

7.访问

  注意访问时输入的URL

  


8.参考文章

  博文1:http://bbs.51cto.com/thread-1131636-1.html  博文2:http://www.blogjava.net/soken/articles/371548.html  博文3:http://wiselyman.iteye.com/blog/2002446
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: