发表于: 2017-06-23 22:51:51

1 1170


一整天都在弄毕设,后天答辩就更没时间写代码了,希望在26号之前把任务2完成。

1.两个spring的小问题,1.@Service注解注到了接口上,而没有注到实现类上,导致spring容器初始化时找不到接口的实现类。
2.跟前面那个差不多,@Repository注解到了dao层的接口上,注解错了,而且我用的mybatis根本没有实现类,导致注入失败,
应该在spring的配置文件中配置好接口的bean,而且和sqlsessionFactory绑定好。


2.在传统的开发过程中,我们的控制CONTROLL层通常需要转向一个JSP视图;但随着WEB2.0相关技术的崛起,我们很多时候只需要返回数据即可,而不是一个JSP页面。
加上用HTTP动词(GET,POST,DELETE,DETC)描述操作,并吐出标准数据格式的数据(JSON/XML),就构成了所谓的REST风格。
SPRING MVC3的
@ResponseBody使Controller直接返回数据,而不是直接指向具体的视图;同时通过
MessageConverter和produces(如produces="text/plain;charset=UTF-8")可以返回各种格式的数据(XML,json,RSS,TEXT,字节流等)
@ResponseBody可以直接返回结果,
而ResponseEntity 可以定义返回的HttpHeaders和HttpStatus
HttpHeaders是HTTP协议标头,HTttpStatus是Http状态码。

3.RequestMapping(value = "/produces", produces = "application/json"):表示将功能处理方法将生产json格式的数据,此时根据请求头中的Accept进行匹配,如请求头“Accept:application/json”时即可匹配;
@RequestMapping(value = "/produces", produces = "application/xml"):表示将功能处理方法将生产xml格式的数据,此时根据请求头中的Accept进行匹配,如请求头“Accept:application/xml”时即可匹配。
此种方式相对使用@RequestMapping的“headers = "Accept=application/json"”更能表明你的目的。

Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。使用起来和tomacat差不多,都是web容器。

下面控制器的接口格式借鉴了@王玉琛师兄的日报

package com.kaibo.task2.controller;

import com.kaibo.task2.pojo.Student;
import com.kaibo.task2.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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.util.UriComponentsBuilder;

import java.util.List;

/**
* Created by hfismyangel@163.com on 2017/6/21.
*/
@Controller
@RequestMapping("/restfulDemo")
public class TestController {
@RequestMapping("/test")
public String testIndex() {
return "addStudent";
   }

@Autowired
   StudentService ss;
   /*
   用HTTP协议里的动词来实现资源的添加,修改,删除等操作。即通过HTTP动词来实现资源的状态扭转:
GET    用来获取资源,
POST  用来新建资源(也可以用于更新资源)
PUT    用来更新资源,
DELETE  用来删除资源。
    */


   //------------------------------getStudnet----------------------------------------------------------------------
   //查询单个学生信息,produces的MediaType属性表示输出json格式的数据,APPLICATION_JSON_VALUE相当于application/json
   @RequestMapping(value = "/user/get/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
//@PathVarible表示从url地址中取值并注入
   public ResponseEntity<Student> getStudent(@PathVariable("id") int id) {
Student student = ss.selectStudent(id);
       if (student == null) {
//ResponseEntity响应实体类,可以定义返回的HttpHeaders和HttpStatus,HttpStatus.NOT_FOUND表示返回状态码404
           return new ResponseEntity<Student>(HttpStatus.NOT_FOUND);
       }
//HttpStatus.OK状态码200
       return new ResponseEntity<Student>(student, HttpStatus.OK);
   }

//-------------------------------addStudnet-----------------------------------------------------------------------
   @RequestMapping(value = "/user/addStudent", method = RequestMethod.POST)
public ResponseEntity<Void> addStudent(@RequestBody Student stu,UriComponentsBuilder ucBuilder) {
if(ss.selectStudent(stu.getId())!=null){
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
       }
ss.addStudent(stu);
       //Spring组件UriComponentsBuilder和UriComponents来组建并编码URI
       HttpHeaders headers = new HttpHeaders();
       headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(stu.getId()).toUri());
       return new ResponseEntity<Void>(HttpStatus.CREATED);
   }

//-------------------------------getAllStudent----------------------------------------------------------------------
  //之前Task1中有220万条数据,虽然List能放得下,但直接全查出来会报内存不足异常,应该在数据库做limit分页
   @RequestMapping(value = "/user/getall", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List> getAllStudent() {
List<Student> studentList = ss.selectAllStudent();
       if (studentList == null) {
return new ResponseEntity<List>(HttpStatus.NOT_FOUND);
       }
return new ResponseEntity<List>(studentList, HttpStatus.OK);
   }

//------------------------------deleteStudent-----------------------------------------------------------------------
   @RequestMapping(value = "/user/delete/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> deleteStudent(@PathVariable("id") int id) {
//调用dao层的delete方法将返回受影响的行数,作为删除成功的依据
       int code = ss.deleteStudent(id);
       if (code != 1) {
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
       }
StringBuffer res = new StringBuffer("delete success!Student id =");
       res.append(id);
       return new ResponseEntity<String>(res.toString(), HttpStatus.NO_CONTENT);
   }

//-----------------------------updateStudent------------------------------------------------------------------------
   @RequestMapping(value = "/user/update", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> updateStudent(@RequestBody Student stu) {
int code = ss.updateStudent(stu);
       if (code != 1) {
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
       }
return new ResponseEntity<String>("Update Success!", HttpStatus.OK);
   }

}

顺便复习了一下spring注入方式,设值注入和构造注入。



返回列表 返回列表
评论

    分享到