我正在尝试用Spring,Java和Tomcat实现我的第一个REST服务。我的项目的目的是实现一个返回使用JSON格式化的数据的URL。 这是我项目的不同元素:
的pom.xml
<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>com.incvision.utils</groupId>
<artifactId>springexample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springexample Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<springframework.version>4.0.6.RELEASE</springframework.version>
<jackson.library>2.6.3</jackson.library>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.library}</version>
</dependency>
</dependencies>
<build>
<finalName>springexample</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<display-name>
Archetype Created Web Application
</display-name>
<servlet>
<servlet-name>
mvc-dispatcher2
</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher2</servlet-name>
<url-pattern>/resthandler/*</url-pattern>
</servlet-mapping>
</web-app>
Servlet mvc-dispatcher2 definition =&gt; mvc-dispatcher2-servlet.xml:
<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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-
3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-
package="com.incvision.utils.resthandler" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
控制器定义: HelloWorld2Controller.java
package com.incvision.utils.resthandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/helloapp")
public class HelloWorld2Controller {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(ModelMap model) {
model.addAttribute("msg", "JCG Hello World!");
return "helloWorld";
}
@RequestMapping(value = "/displaymessage/{msg}", method = RequestMethod.GET)
public String displayMessage(@PathVariable String msg, ModelMap model) {
model.addAttribute("msg", msg);
return "helloWorld";
}
}
SaveImageAjaxController.java
package com.incvision.utils.resthandler;
import java.io.FileOutputStream;
import java.util.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.incvision.utils.general.AjaxResponseBody;
@Controller
@RequestMapping("/webagent")
public class SaveImageAjaxController {
@RequestMapping(value = "/saveimage", method = RequestMethod.GET)
@ResponseBody
public AjaxResponseBody getSearchResultViaAjax(@RequestBody String imageData) {
System.out.println("Appel de SaveImageAjaxController à /webagent/saveimage");
AjaxResponseBody result = new AjaxResponseBody();
byte[] data =
Base64.getDecoder().decode(imageData.getBytes());
try (FileOutputStream fos = new FileOutputStream("/Users/pjdoe/Documents/moninamge.png")) {
fos.write(data);
fos.close();
} catch(Exception e) {e.printStackTrace();}
result.setCode("200");
result.setMsg("L'image a été sauvegardé.");
return result;
}
}
jsp:HelloWorld.jsp
<%@ 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>
<body>
<h1>Spring 4.0.2 MVC web service</h1>
<h3>Your Message : ${msg}</h3>
</body>
</html>
我有两项服务。第一个服务显示带参数的hello消息。它映射到网址http://localhost:8080/SpringRestHandler/resthandler/helloapp/hello 第二个服务保存使用ajax上传的图像。它映射在网址http://localhost:8080/SpringRestHandler/resthandler/webagent/saveimage
上这两个请求检索到404错误。请求由Postman执行。 我的实施中有什么问题? 非常感谢您的回复。