使用Spring Boot的简单RESTFul Web服务

时间:2017-09-06 15:18:04

标签: spring-boot

我是Spring的新手。我写了一个简单的代码,在GET中没有任何问题,但是当我想要POST并添加一个学生时,它会显示Whitelabel Error Page。这是我的代码



@SpringBootApplication
public class Test1Application {
	public static HashMap<Long, Student> hmstudent;

	public static void main(String[] args) {
		hmstudent=new HashMap<Long,Student>();
		Student one=new Student("Ali","Math");
		hmstudent.put(one.getId(),one);
		SpringApplication.run(Test1Application.class, args);
		Student two=new Student("Mohamad","Persian");
		hmstudent.put(two.getId(),two);
	}
	@RestController
	@RequestMapping(value="/rest/student")
	class StudentService{

		@RequestMapping(value="/",method = RequestMethod.GET)
		public HashMap<Long,Student> getAllStudents(){
			return Test1Application.hmstudent;
		}

	}
	@RequestMapping(value="/add",method = RequestMethod.POST)
	@ResponseBody
	public Student addStudent(@RequestParam(value="name") String name
			,@RequestParam(value="subject",defaultValue = "unknown") String subject){

		Student student1=new Student(name,subject);
		Test1Application.hmstudent.put(student1.getId(),student1);
		return student1;
	}
}
&#13;
&#13;
&#13;

0 个答案:

没有答案