步骤 2 : 模仿和排错 步骤 3 : 删除 App 步骤 4 : LogPrintStream 步骤 5 : Application 步骤 6 : application.properties 步骤 7 : 指数代码文件 步骤 8 : IndexService 步骤 9 : IndexController 步骤 10 : 启动 Application 步骤 11 : 增加更多的指数
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
启动 Application 然后访问地址: http://127.0.0.1:9888/trend/indexs
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
创建类,这个是一个工具类,方便可以在System.out.prinlnt 的时候打印类和行信息,方便调试。
package org.how2j.trend.util;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LogPrintStream extends PrintStream{
public static boolean log = true;
public void println() {
println("");
}
public void println(boolean x) {
println(String.valueOf(x));
}
public void println(char x) {
println(String.valueOf(x));
}
public void println(int x) {
println(String.valueOf(x));
}
public void println(long x) {
println(String.valueOf(x));
}
public void println(float x) {
println(String.valueOf(x));
}
public void println(double x) {
println(String.valueOf(x));
}
public void println(char[] x) {
println(String.valueOf(x));
}
public LogPrintStream(OutputStream out) {
super(out);
}
public LogPrintStream() {
this(System.out);
}
public static void closeLog(){
log= false;
}
public void println(Object x){
println(String.valueOf(x));
}
public void println(String msg){
if(!log){
super.println(msg);
return;
}
try {
throw new Exception();
} catch (Exception e) {
StackTraceElement[] stes = e.getStackTrace();
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
StackTraceElement s= getTriggerTraceElement(stes);
String logFormat = "%s (%s:%d) %s() - %s%n";
super.printf(logFormat, time,s.getFileName(), s.getLineNumber(), s.getMethodName(), msg);
}
}
private StackTraceElement getTriggerTraceElement(StackTraceElement[] stes) {
for (StackTraceElement s : stes) {
String clazzName = s.getClassName();
if(clazzName.equals(LogPrintStream.class.getName()))
{
continue;
}
return s;
}
return null;
}
public static void init() {
System.setOut(new LogPrintStream());
}
}
创建启动类 Application
1. 使用 NetUtil.isUsableLocalPort 来判断 9888这个端口是否可用,如果已经使用了,就显示错误信息,表示端口已经被占用 2. 通过 SpringApplication.run(Application.class, args); 运行 Applicatoin 3. 打印 http://127.0.0.1:9888/trend/ 这个地址 package org.how2j.trend;
import java.io.IOException;
import java.net.URISyntaxException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.how2j.trend.util.LogPrintStream;
import cn.hutool.core.util.NetUtil;
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) throws IOException, URISyntaxException {
String url = "http://127.0.0.1:9888/trend/";
int port = 9888;
if(NetUtil.isUsableLocalPort(port)) {
LogPrintStream.init();
System.setProperty("java.awt.headless", "false");
SpringApplication.run(Application.class, args);
System.out.println(url);
}
else {
System.err.printf("本项目端口号 %d 已经被占用,无法启动%n", port);
}
}
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
在 resources 这个目下创建文件: application.properties
#thymeleaf部分 对 thymeleaf 进行配置,后续的页面部分,我们会使用 thymeleaf 来进行展示。 #context部分 配置上下文为 : /trend 配置端口号为 9888 #compression 部分 配置压缩相关信息 #thymeleaf
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
#context
server.context-path=/trend
server.port=9888
#compression
server.compression.enabled=true
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json
#thymeleaf spring.thymeleaf.mode=LEGACYHTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false #context server.context-path=/trend server.port=9888 #compression server.compression.enabled=true server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json
参考图片,在 main 目录下创建 webapp, 接着创建 indices 目录。
然后创建 codes.json 文件,里面的内容就是指数代码和指数名称。 [
{
"code": "399975",
"name": "证券公司"
},
{
"code": "000015",
"name": "红利指数"
},
{
"code": "000016",
"name": "上证50"
},
{
"code": "399812",
"name": "养老产业"
},
{
"code": "399971",
"name": "中证传媒"
},
{
"code": "399006",
"name": "创业扳指"
},
{
"code": "000992",
"name": "全指金融"
},
{
"code": "000991",
"name": "全指医药"
},
{
"code": "000300",
"name": "沪深300"
},
{
"code": "000827",
"name": "中证环保"
},
{
"code": "000905",
"name": "中证500"
},
{
"code": "000001",
"name": "上证指数"
},
{
"code": "000852",
"name": "中证1000"
}
]
创建类 IndexService ,用于获取 codes.json 的数据,并转化为 List集合。
package org.how2j.trend.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class IndexService {
@Autowired RestTemplate restTemplate;
public List<Map> listIndex(){
List<Map> indexs= restTemplate.getForObject("http://127.0.0.1:9888/trend/indices/codes.json",List.class);
return indexs;
}
public String getIndexName(String code){
List<Map> indexes = listIndex();
for (Map index : indexes) {
if(code.equals(index.get("code")))
return (String) index.get("name");
}
return "unknown";
}
}
创建 IndexController,用于映射路径 /indexs
package cn.how2j.trend.web;
import java.util.List;
import java.util.Map;
import cn.how2j.trend.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Autowired
IndexService indexService;
@GetMapping("/indexs")
public List<Map> list() throws Exception {
List<Map> result = indexService.listIndex();
return result;
}
}
启动 Application ,并访问地址:
http://127.0.0.1:9888/trend/indexs 就能看到如图所示的效果。
学习是为了应用,后续当需要增加更多指数的时候,修改指数代码文件 即可。 记得要符合json格式,否则无法识别。
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
![]()
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|