# 利用 Solon-web 框架写一个 Hello World
**Solon 项目的开源地址:**
[https://gitee.com/noear/solon](https://gitee.com/noear/solon)
> 最近看了不少别人写的各种框架的 `Hello world` 示例,有些看起来,真的很复杂。
>
> 今天,我们用号称简单到不能再简单的 `Solon` 框架也写一个 `Hello world`(确实是0配置,除了meven)
### 一、写代码
#### 1. 用 IntelliJ IDEA 新建一个 Meven 空项目 `helloworld`
#### 2. 在 `pom.xml` 文件里添加框架依赖
```xml
<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-web</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
```
#### 3. 添加应用启动入口 `/src/main/java/helloworld/App.java`
```
package helloworld;
import org.noear.solon.Solon;
public class App {
public static void main(String[] args) {
Solon.start(App.class, args);
}
}
```
#### 4. 添加控制器 `/src/main/java/helloworld/controller/HelloController.java`
```
package helloworld.controller;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.ModelAndView;
import java.util.HashMap;
import java.util.Map;
@Controller
public class HelloController {
/**
* 这是直接返回值
* */
@Mapping("/")
public String hello() {
return "Hello world!";
}
/**
* 这是返回个对象(以json形式)
* */
@Mapping("/json")
public Map hello_json() {
Map<String,Object> map = new HashMap<>(); //实体也ok
map.put("message", "Hello world!");
return map;
}
/**
* 这是用 FreeMarker 进行渲染(你想用别的引擎?可以随便换,比如:Enjoy)
* */
@Mapping("/ftl")
public ModelAndView hello_ftl() {
ModelAndView mv = new ModelAndView("hello.ftl");
mv.put("message", "Hello world!");
return mv;
}
}
```
#### 5.再添加个模板文件 `/src/main/resources/templates/hello.ftl`
```html
<html>
<body>
${message!}
</body>
</html>
```
#### 6. 运行 `App.main()`
对着 App.java 文件,右键,选择:`Run 'App.main()'`
#### 7. 用浏览器打开:`http://localhost:8080/`
#### 8. 再用wrk测试一下qps
测试代码:`wrk -t10 -c200 -d30s --latency "http://127.0.0.1:8080/"` ;大概有 4.5万的qps,不错的
听说切换到嵌入式 `jetty` 有 6.5万左右;切换到嵌入式 `undertow` 有 8万左右。
`solon-mvc` 默认配的是 jlhttp,不过它小巧,只有0.1m
到此,写代码的事儿。搞定了!
### 二、发布服务包
#### 1. 再修改下pom.xml文件,添加打包配置
```xml
<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-web</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>demo3001.WebApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
#### 2. 使用meven打包命令,打包成:`helloworld.jar`
OK,传说中的一个微形小服务出来了。
#### 3. 用命令运行 `java -jar helloworld.jar` (不用容器,不用容器,不用容器......)
再用浏览器打开:`http://localhost:8080/`
### 三、DEMO源码
[源码:demo02.solon_helloworld](https://gitee.com/noear/solon_demo/tree/master/demo02.solon_helloworld)
没有合适的资源?快使用搜索试试~ 我知道了~
noear::Solon 项目,官网配套示例
共2000个文件
java:1138个
h:225个
xml:197个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 39 浏览量
2024-04-08
09:41:50
上传
评论
收藏 7.36MB ZIP 举报
温馨提示
noear::Solon 项目,官网配套示例。启动快 5 ~ 10 倍;qps 高 2~ 3 倍;运行时内存节省 1/3 ~ 1/2;打包可以缩到 1/2 ~ 1/10
资源推荐
资源详情
资源评论
收起资源包目录
noear::Solon 项目,官网配套示例 (2000个子文件)
bad_utf8_string 3B
descriptor.pb.cc 444KB
descriptor_unittest.cc 299KB
descriptor.cc 282KB
map_test.cc 135KB
message_differencer_unittest.cc 114KB
generated_message_reflection.cc 107KB
text_format.cc 93KB
protostream_objectwriter_test.cc 92KB
message_differencer.cc 83KB
extension_set.cc 81KB
type.pb.cc 80KB
text_format_unittest.cc 78KB
wrappers.pb.cc 72KB
wire_format.cc 66KB
repeated_field_unittest.cc 66KB
extension_set_unittest.cc 55KB
arena_unittest.cc 54KB
wire_format_unittest.cc 52KB
generated_message_reflection_unittest.cc 50KB
api.pb.cc 49KB
protostream_objectwriter.cc 47KB
coded_stream_unittest.cc 46KB
lite_unittest.cc 41KB
struct.pb.cc 39KB
protostream_objectsource.cc 39KB
tokenizer.cc 37KB
field_mask_util_test.cc 36KB
descriptor_database.cc 36KB
protostream_objectsource_test.cc 35KB
tokenizer_unittest.cc 35KB
json_stream_parser.cc 32KB
zero_copy_stream_unittest.cc 31KB
coded_stream.cc 31KB
dynamic_message.cc 31KB
json_stream_parser_test.cc 30KB
repeated_field_reflection_unittest.cc 30KB
proto_writer.cc 28KB
wire_format_lite.cc 27KB
generated_message_util.cc 27KB
descriptor_database_unittest.cc 25KB
field_mask_util.cc 25KB
unknown_field_set_unittest.cc 24KB
proto3_arena_unittest.cc 24KB
printer_unittest.cc 23KB
no_field_presence_test.cc 22KB
io_win32_unittest.cc 22KB
default_value_objectwriter.cc 22KB
map_field.cc 22KB
json_util_test.cc 21KB
message_lite.cc 21KB
extension_set_heavy.cc 20KB
reflection_ops_unittest.cc 20KB
field_comparator_test.cc 20KB
parse_context.cc 19KB
type_resolver_util_test.cc 17KB
reflection_ops.cc 17KB
time_util.cc 16KB
map_field_test.cc 16KB
time_util_test.cc 16KB
generated_message_tctable_lite.cc 16KB
arena.cc 16KB
json_escaping.cc 14KB
datapiece.cc 14KB
type_resolver_util.cc 14KB
utility.cc 14KB
any.pb.cc 13KB
io_win32.cc 13KB
printer.cc 13KB
zero_copy_stream_impl_lite.cc 13KB
dynamic_message_unittest.cc 13KB
message.cc 13KB
preserve_unknown_enum_test.cc 12KB
timestamp.pb.cc 12KB
source_context.pb.cc 12KB
field_mask.pb.cc 11KB
zero_copy_stream_impl.cc 11KB
json_util.cc 11KB
unknown_field_set.cc 10KB
gzip_stream.cc 10KB
json_objectwriter_test.cc 10KB
googletest.cc 10KB
arenastring.cc 10KB
empty.pb.cc 9KB
field_comparator.cc 9KB
field_mask_utility.cc 8KB
type_info.cc 7KB
default_value_objectwriter_test.cc 7KB
file.cc 7KB
json_objectwriter.cc 6KB
repeated_field.cc 6KB
arenastring_unittest.cc 6KB
type_info_test_helper.cc 5KB
delimited_message_util.cc 5KB
generated_message_table_driven_lite.cc 4KB
generated_message_table_driven.cc 4KB
map_lite_test_util.cc 4KB
delimited_message_util_test.cc 4KB
any_lite.cc 4KB
drop_unknown_fields_test.cc 4KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6732
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功