发表于: 2017-08-21 22:55:48

1 1031


一、今日完成

1.学习MVC框架常用注解:@Controller、@RequestMapping、@ModeAttribute、@pATHVariable、@ControllerAdvice和@ExceptionHandler等的基本概念

2.参考教程,使用IDEA创建一个基于Maven的REST接口风格的web服务

1)REST意思是表述性状态转移(REpresentational State Transfer),是一种基于HTTP的结构规则,表示被操作的资源。

2)创建web project

i.向pom.xml添加spring-webmvc、spring-core、spring-beans、spring-context和spring-web这五个依赖(spring-webmvc组件依赖后四项),目前较为稳定的版本是“4.3.9.RELEASE”,然后添加jackson-core和jack-databind两项依赖(根据教程的说法,这两个依赖项用来处理序列化/反序列化以及将对象映射到JSON的框架,不明觉厉),使用版本是“2.8.7”;

ii.在src/main/java/com.task.beginningspring.REst/domain目录下,新建域类User:

public class User {

private  int id;
private  String name;

public  User() {}

public User(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public String getName() {
return name;
}
}

包含了id和name两个成员变量,以及同名构造方法、同名含参setter方法与getter方法。

iii.在src/main/java/com.task.beginningspring.REst/dao目录下新建一个存储库类UserRepository,并在一个映射中存储两个用户,调用@PostConstruct方法对用户进行实例化。

@Repository
public class UserRepository {

private Map<Integer, User> users = new HashMap<Integer, User>();

@PostConstruct
   public void setup() {
users.put(1, new User(1, "Mert Caliskan"));
users.put(2, new User(2, "Kenan Sevindik"));
}

public void save(User user) {
users.put(user.getId(), user);
}

public List<User> findAll() {
return new ArrayList<User>(users.values());
}

public User find(int id) {
return users.get(id);
}

public void update(int id, User user) {
users.put(id, user);
}

public void delete(int id) {
users.remove(id);
}
}

提供了4种方法进行CRUD操作。

iv.src/main/java/com.task.beginningspring.REst/controller目录下新建UserController类,作为 REST Web服务控制器,提供与UserRepository类对应的四个方法。

@RestController
@RequestMapping("/rest")
public class UserRestController {

@Autowired
   private UserRepository userRepository;

@RequestMapping(value = "/users", method= RequestMethod.POST)
public void save(@RequestBody User user) {
userRepository.save(user);
}

@RequestMapping(value = "/users", method=RequestMethod.GET)
public List<User> list() {
return userRepository.findAll();
}

@RequestMapping(value="/users/{id}", method=RequestMethod.GET)
public User get(@PathVariable("id") int id) {
return userRepository.find(id);
}

@RequestMapping(value="/users/{id}", method=RequestMethod.PUT)
public void update(@PathVariable("id") int id, @RequestBody User user) {
userRepository.update(id, user);
}

@RequestMapping(value="/users/{id}", method=RequestMethod.DELETE)
public ResponseEntity<Boolean> delete(@PathVariable("id") int id) {
userRepository.delete(id);
return new ResponseEntity<Boolean>(Boolean.TRUE, HttpStatus.OK);
}
}

v.在src/main/webapp/WEB-INF目录下,创建配置文件springmvc-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"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      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.task.beginningspring.REST"/>
<context:annotation-config  />

<mvc:annotation-driven/>
</beans>

vi.向web.xml中使用URL映射定义的Dispatcher Servlet。

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        version="3.1">

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

vii.接下来在Tomcat上部署应用程序。

3.在tomcat部署应用程序

1)使用mvn clean 、install命令打war包,把相应war包拷贝到${TOMCAT_HOME}/webapps目录下,启动${TOMCAT_HOME}/bin/startup.bat,tomcat会自动扫描到并解压war包,容器开始运行;

2)IDEA2017里集成了Tomcat,可以通过配置local Tomcat Server把Tomcat的服务组件添加到IDEA的功能区,直接在IDEA中运行该容器。

3)其他两个使用静态方法配置上下文来实现在Tomcat上配置应用程序的方法,尝试过,因为配置元素的值出了问题,最后并不成功,就不赘述和推荐了。

4.学习依赖注入的setter注入和构造函数注入,具体实现代码比较简单,不作说明;其中setter注入是在Bean实例创建完毕后执行,构造函数注入是组件创建期间执行。


二、明日计划

因处理个人私事的缘故,明日请假。


三、遇到的问题

1.单独启动${TOMCAT_HOME}/bin/startup.bat,浏览器打开“localhost:8080” 会提示404错误,;经过检查发现8080 port 并未被占用,tomcat相关服务处于运行状态,浏览器打开“localhost“结果正常。最终通过重装Tomcat服务来解决这一问题,但是并未找到404问题所在。


四、收获

1.学会如何使用Tomcat部署应用;

2.学习创建一个简单REST Web的流程。






返回列表 返回列表
评论

    分享到