1.Maven Dependency
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.fool.springmvc</groupId> <artifactId>springmvc</artifactId> <name>springmvc-test</name> <packaging>war</packaging> <version>1.0.0-BUILD-SNAPSHOT</version> <properties> <java-version>1.7</java-version> <org.springframework-version>3.2.0.RELEASE</org.springframework-version> <org.aspectj-version>1.7.1</org.aspectj-version> <org.slf4j-version>1.7.2</org.slf4j-version> <log4j-version>1.2.17</log4j-version> <junit-version>4.11</junit-version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${org.slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j-version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j-version}</version> <exclusions> <exclusion> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> <scope>runtime</scope> </dependency> <!-- @Inject --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2.1-b03</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit-version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <additionalProjectnatures> <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature> </additionalProjectnatures> <additionalBuildcommands> <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand> </additionalBuildcommands> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>org.test.int1.Main</mainClass> </configuration> </plugin> </plugins> </build> </project>
2.Project Directory
3.Rest Controller
For Spring RESTful, you need add @PathVariable, @RequestMapping and @RequestMethod annotations.
package org.fool.springmvc.helloworld; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/rest/helloworld") public class HelloRestController { @RequestMapping(value = "/{name}", method = RequestMethod.GET) public String getBook(@PathVariable String name, Model model) { model.addAttribute("book", name); return "list"; } @RequestMapping(value = "/", method = RequestMethod.GET) public String getDefaultBook(Model model) { model.addAttribute("book", "this is default book"); return "list"; } }
4.JSP Views
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring 3 MVC Rest Hello World</title> </head> <body> <h1>Book Name : ${book}</h1> </body> </html>
5.Result
相关推荐
Spring3MVC-REST-HelloWorld 是一个基础的示例,用于展示如何在Spring框架的MVC模块中实现RESTful Web服务。这个实例是初学者理解Spring MVC与REST结合使用的理想起点。REST(Representational State Transfer)是一...
查看`spring-boot-helloworld`压缩包中的源码,可以看到项目的基本结构和代码实现。`pom.xml`是Maven的配置文件,定义了项目依赖;`src/main/resources`存放资源配置文件;`src/main/java`是代码目录。 **工具使用*...
本教程将引导你通过一个简单的 Spring MVC 开发环境的设置,以及如何创建和运行你的第一个 "Hello, World!" 示例。 ### 一、Spring MVC 概述 Spring MVC 提供了一个灵活的架构,允许开发者选择他们喜欢的技术来...
在Spring REST中,我们利用Spring MVC的@Controller和@RequestMapping注解来定义REST端点。例如,你可以创建一个名为"HelloController"的类,用@RestController注解标记,然后定义一个返回"Hello, World!"的GET请求...
在这个基础文档范本中,我们学习了如何创建 Spring Boot 项目、理解其基本结构以及编写简单的 REST API。随着对 Spring Boot 更深入的了解,我们可以利用其强大的特性,如自动配置、健康检查、Actuator 等,来构建更...
这个项目可能已经包含了所需的依赖,如 `spring-boot-starter-web`,它包含了 Spring MVC 和 Tomcat 服务器。 2. **定义 RESTful 控制器**:在 Java 代码中,你可以创建一个带有 `@RestController` 注解的类,它...
例如,如果希望处理URL为`/helloWorld`的GET请求,则可以在控制器类中添加如下的方法: ```java @GetMapping("/helloWorld") public String handleHelloWorld() { // 处理逻辑 return "success"; } ``` 此外,还...
在"spring-rest-boot-demo"项目中,开发者通常会使用Spring MVC作为构建REST服务的基础。Spring MVC提供了一套完整的模型-视图-控制器架构,用于处理HTTP请求和响应。通过定义控制器方法,我们可以轻松地创建处理...
本节将通过一个简单的HelloWorld示例来介绍SpringMVC的基本配置和使用方法。 - **步骤1:添加依赖jar包** 需要在项目中引入以下SpringMVC相关的jar包: - `commons-logging-1.1.3.jar` - `spring-aop-4.0.0....
创建一个 "Hello World" Web SpringBoot 项目,首先你需要确保安装了 JDK 和 Maven,因为 SpringBoot 基于 Java 并使用 Maven 或 Gradle 进行构建。接下来,按照以下步骤操作: 1. **初始化项目**: 使用 Spring ...
<bean id="restApplication" class="com.example.MyRestApplication"> <bean id="defaultHost" class="org.restlet.ext.spring.SpringHost"> <value>org.restlet.ext.jetty.JettyServer ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
可以使用JAX-RS(Java API for RESTful Web Services)框架如Jersey或Spring MVC实现。 2. 数据绑定:通过JAXB(Java Architecture for XML Binding)可以将XML数据自动转换为Java对象,简化数据处理。 3. 安全性...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); }); ``` 3. **中间件使用:** - 中间件用于处理 HTTP 请求并执行其他任务,...