我正在尝试使用spring创建一个简单的restful Web服务代码。 这是我到目前为止创建的完整代码:
的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" 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>TemporaryTry01</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<context:component-scan base-package="com.arman"/>
</beans>
Student.java :
package com.arman.dta;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Student {
@JsonProperty("student_name")
private String studentName;
private long mobileNo;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public long getMobileNo() {
return mobileNo;
}
public void setMobileNo(long mobileNo) {
this.mobileNo = mobileNo;
}
}
HelloController.java
package com.arman.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.MediaType;
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;
import com.arman.dta.Student;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping(value="/students", method=RequestMethod.GET) // already Tried to add produces attribute explicitly with MediaType.APPLICATION_JSON_VALUE but that too is not working
public ArrayList<Student> getStudentList(){
Student st = new Student();
st.setStudentName("Arman");
st.setMobileNo(903434555L);
Student st1 = new Student();
st1.setStudentName("Avasthi");
st1.setMobileNo(779232245L);
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(st);
studentList.add(st1);
return studentList;
}
}
我已添加所有弹簧罐(版本4.2.2),公共记录罐和3杰克逊罐子,即: 杰克逊的注解 - 2.8.1.jar 杰克逊核心2.8.1.jar 杰克逊 - 数据绑定-2.8.1.jar
使用Eclipse Oxygen IDE,我没有遇到编译时问题。但是当我运行项目(在端口号7070 上的Tomcat 8上)并尝试访问:
http://localhost:7070/TemporaryTry01/students
我收到以下错误页面
在堆栈跟踪中显示:
2017年12月10日下午8:51:15 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver logException 警告:处理程序执行导致异常:找不到可接受的表示