盒子
盒子
文章目录
  1. Servlet的使用
  2. Spring Boot
    1. 跨域访问
    2. 返回界面

我的第一个接口

Servlet的使用

通过IDEA新建Sevlet工程:

Java -> Web Application (选上”create web.xml”),即可得如下目录结构:

Sevlet工程结构

在src中创建继承于HttpServlet的类,重写doGet和doPost方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SeatOverrMain extends HttpServlet {
public SeatOverrMain() {
super();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletOutputStream out=resp.getOutputStream();
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
String param=req.getParameter("Seat");
if(param.equals("Seat")){
out.print("Success");
}else{
out.print("fail");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}

注意要将doGet和doPost方法中的super语句去掉,不然访问时会报405错误

HttpServlet所在的jar包为:“servlet-api-6.0.41.jar”,可在jar search中获得

然后需要搭建Tomcat服务器,步骤如下:

先下载Tomcat的压缩包解压出来,在IDEA运行按钮处打开Edit Configuration窗体:

Defauts -> Tomcat Server -> Local -> server -> Configure…

在Toncat Home中选中刚刚解压出来的Tomcat文件夹,回到Edit Configuration窗体:

Defauts所在栏中会多出一个Tomcat Server,选中然后在右边窗体中选择Deployment:

再添加[项目名]:war exploded,然后ok。在刚刚运行处便会出现Tomcat图标此时即可运行

Spring Boot

很牛逼的一个框架,刚开始被她的简单惊到了,O O

具体用法参见:

构建微服务:Spring boot 入门篇

跨域访问

需要新建过滤类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package io.github.grooters.seatOccupied.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest reqs = (HttpServletRequest) servletRequest;
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
filterChain.doFilter(reqs, response);
}
@Override
public void destroy() {}
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
}

返回界面

添加依赖:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

application.properties中添加配置:

1
2
3
4
# 定位模板的目录,在resouces/static中
spring.mvc.view.prefix=/
# 给返回的页面添加后缀名
spring.mvc.view.suffix=.html

Controller:

1
2
3
4
5
6
7
8
9
10
package io.github.grooters.seatOccupied.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Index {
@GetMapping("/index")
public String greet() {
return "index";
}
}
支持一下
扫一扫,支持Grooter
  • 微信扫一扫
  • 支付宝扫一扫