我正在尝试使用此链接创建Web sersive http://www.programming-free.com/2014/03/spring-mvc-40-restful-web-service-json.html 我按照所有步骤操作,因为我需要静态数据,所以我不想连接数据库。但是我收到错误
我按照步骤
1)新增 - >动态网络项目
2)添加url中提供的所有库
共享记录-1.1.1.jar
jackson-annotations-2.0.0.jar
jackson-core-2.0.0.jar
jackson-databind-2.0.0.jar
jstl-1.2.jar
mysql-connector-java-3.1.12.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-framework-bom-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
的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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringServiceJsonSample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
其余-servlet.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.programmingfree.springservice.controller" />
<mvc:annotation-driven />
</beans>
controller.js
package com.programmingfree.springservice.controller;
import java.util.HashMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/service/user/")
public class SpringServiceController {
HashMap<String, String> userService=new HashMap<String, String>();
@RequestMapping(value = "/test", method = RequestMethod.GET,headers="Accept=application/json")
public HashMap getUser(@PathVariable int id) {
userService.put("name", "hello");
return userService;
}
}
当我点击服务器时 的 http://localhost:8080/dddddd/service/user/test 它显示客户端发送的请求在语法上是不正确的()。
答案 0 :(得分:1)
你的mpaping需要一个PathVariable id,但是你的映射模式没有定义它
将其添加到请求映射或从参数中删除它,映射将成功。以下是已删除PathVariable的示例
@RequestMapping(value = "/test", method = equestMethod.GET,headers="Accept=application/json")
public HashMap getUser() {
}