SpringBoot使用入门和案例实现

1. 在pom.xml中添加依赖

步骤如下:

  1. 添加springboot的parent依赖
  2. 添加springboot的starter-web依赖。还有很多针对其它场景的starter启动器
  3. 修改mysql的版本号
  4. 添加springboot的打包插件

内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hh</groupId>
    <artifactId>springbootTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
    </parent>

    <properties>
        <!-- 修改mysql的版本号 -->
        <mysql.version>8.0.25</mysql.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
		<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>${shade.plugin.version}</version>
				<dependencies>
					<dependency>
						<groupId>org.springframework.boot</groupId>
						<artifactId>spring-boot-maven-plugin</artifactId>
						<version>${spring.boot.version}</version>
					</dependency>
				</dependencies>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<artifactSet>
								<excludes>
									<exclude></exclude>
								</excludes>
							</artifactSet>
							<filters>
								<filter>
									<artifact>*:*</artifact>
									<excludes>
										<exclude>META-INF/*.SF</exclude>
										<exclude>META-INF/*.RSA</exclude>
										<exclude>META-INF/*.DSA</exclude>
										<!-- 解决java -classpath ... class运行错误:ERROR StatusLogger Unrecognized format specifier -->
										<exclude>**/Log4j2Plugins.dat</exclude>
									</excludes>
								</filter>
							</filters>
							<transformers>
								<transformer
										implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
									<resource>reference.conf</resource>
								</transformer>
								<transformer
										implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
									<resource>META-INF/spring.factories</resource>
								</transformer>
								<transformer
										implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>
								<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass></mainClass>
									<!-- 解决: WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance -->
									<manifestEntries>
										<Multi-Release>true</Multi-Release>
									</manifestEntries>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
        </plugins>
    </build>

</project>

2. 编辑resources/application.properties

该配置文件的名称是约定好的。修改tomcat的端口号,如下所示:

# 修改tomcat的端口号
server.port = 8088

配置文件的值会绑定对应的类上,类会在IOC容器中创建对象

也可以定义配置文件application.yaml。会和application.properties进行配置合并,但application.properties的优先级更高

3. 编写springboot的主程序

注解说明:

  • @SpringBootApplication:表明这是一个springboot应用。会自动扫描同一目录下的其它所有文件夹和文件

如下所示:

package com.hh.springbootTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// springboot的程序入口,表明这是一个springboot应用
// 默认会自动扫描同一目录下的其它所有文件夹和文件
@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {

        SpringApplication.run(MyApplication.class, args);

    }
}

4. 编写Controller程序

注解说明:

  • @Controller:表明这是一个控制器
  • @ResponseBody:表示请求该地址会有数据返回
  • @RequestMapping:表示请求的具体地址
  • @RestController:等同于@ResponseBody + @Controller

如下所示:

package com.hh.springbootTest.myController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

// @ResponseBody       // 也可以放在函数上面,单独修饰函数
// @Controller

@RestController     // 等同于:@ResponseBody + @Controller
public class HelloController {

    @RequestMapping("/hello")
    public String helloHandle() {
        return "hello springboot";
    }

}

5. IDEA本地测试

直接运行MyApplication.java程序,然后用浏览器访问http://localhost:8088/hello。显示的效果如下:

IDEA本地测试

6. 打包上传到服务器运行

mvn clean package命令进行打包,然后将jar包上传到服务器,用java命令java -jar springbootTest-1.0-SNAPSHOT.jar以前台运行的方式启动应用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-boot2.0全新教程实例20例.zip - [spring-boot-helloWorld](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-helloWorld):Spring Boot 的 hello World 版本 - [spring-boot-web](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web):Spring Boot Web 开发综合示例 - [spring-boot-redis](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-redis):Spring Boot 集成 Redis 示例 - [spring-boot-jpa](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa):Spring Boot 使用 Jpa 各种示例 - [spring-boot-mybaits-annotation](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis/spring-boot-mybatis-annotation):注解版本 - [spring-boot-mybaits-xml](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis/spring-boot-mybatis-xml):Xml 配置版本 - [spring-boot-mybatis-xml-mulidatasource](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis/spring-boot-mybatis-xml-mulidatasource):Spring Boot + Mybatis (Xml 版) 多数据源最简解决方案 - [spring-boot-mybatis-annotation-mulidatasource](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis/spring-boot-mybatis-annotation-mulidatasource):Spring Boot + Mybatis(注解版)多数据源最简解决方案 - [spring-boot-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-thymeleaf):Spring Boot 使用 Thymeleaf 详细示例 - [spring-boot-jpa-thymeleaf-curd](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa-thymeleaf-curd):Spring Boot + Jpa + Thymeleaf 增删改查示例 - [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-rabbitmq):Spring Boot 和 Rabbitmq 各种消息应用案例 - [spring-boot-scheduler](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-scheduler):Spring Boot 和定时任务案例 - [spring-boot-mail](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mail):Spring Boot 和邮件服务 - [spring-boot-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mongodb/spring-boot-mongodb):Spring Boot 和 Mongodb 的使用 - [spring-boot-multi-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mongodb/spring-boot-multi-mongodb):Spring Boot 和 Mongodb 多数据源的使用 - [spring-boot-package-war](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package-war): Spring Boot 打包成 War 包示例 - [spring-boot-shiro](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-shiro):Spring Boot 整合 Shiro Rbac 示例 - [spring-boot-file-upload](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-file-upload):使用 Spring Boot 上传文件示例 - [spring-boot-fastDFS](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-fastDFS):Spring Boot 整合 FastDFS 示例 - [spring-boot-actuator](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-actuator):Spring Boot Actuator 使用示例 - [spring-boot-admin-simple](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-admin-simple):Spring Boot Admin 的使用示例
下面是一个使用Spring Boot搭建WebService的简单入门案例: 1.创建Spring Boot项目,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 2.创建一个WebService类,在其中定义需要暴露的方法: ```java @WebService public interface HelloWorldService { @WebMethod String sayHello(String name); } ``` ```java @WebService(endpointInterface = "com.example.demo.webservice.HelloWorldService") public class HelloWorldServiceImpl implements HelloWorldService { @Override public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 3.配置WebService的端点和实现类: ```java @Configuration public class WebServiceConfig { @Bean public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet() { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(new AnnotationConfigApplicationContext(WebServiceConfig.class)); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean<>(servlet, "/ws/*"); } @Bean(name = "helloWorld") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema xsdSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("HelloWorldPort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://www.example.com/demo/webservice"); wsdl11Definition.setSchema(xsdSchema); return wsdl11Definition; } @Bean public XsdSchema xsdSchema() { return new SimpleXsdSchema(new ClassPathResource("hello.xsd")); } } ``` 4.创建XSD文件定义WebService的参数和返回值: ```xml <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/demo/webservice" xmlns:tns="http://www.example.com/demo/webservice" elementFormDefault="qualified"> <xs:element name="sayHelloRequest"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="sayHelloResponse"> <xs:complexType> <xs:sequence> <xs:element name="result" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> ``` 5.启动应用程序,访问http://localhost:8080/ws/helloWorld.wsdl,将会看到生成的WSDL文件。 6.使用SOAPUI等工具测试webservice。 以上就是一个简单的Spring Boot Webservice入门案例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值