SpringBoot3快速入门案例

一、springboot概述

1、Spring缺点

配置繁琐、依赖繁琐;

2、SpringBoot功能

自动配置、起步依赖;

辅助功能:嵌入服务器、安全、指标,健康检测、外部配置等;

3、小结

SpringBoot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式。


二、入门案例

1、要求

搭建SpringBoot工程,定义HelloController.Hello()方法,返回“Hello SpringBoot!”。  

2、实现步骤

①创建Maven项目

②导入SpringBoot起步依赖( <parent><parent/> 、<dependencies><dependencies/>)

③定义Controller

④编写引导类

⑤测试

3、具体实现

①打开IDEA,创建新工程->空工程;

②在创建好的空工程里New Module;(Build System要选择 Maven

③在新建Module的Pom文件里添加以下(可以下载Maven Search插件,在Tools工具栏里打开可以搜索并复制对应的配置依赖代码):

<parent>
 <!--springboot-->
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>3.0.5</version>
</parent>
​
<!--导入对应的启动器即可-->
<dependencies>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
</dependencies>

④新建Main类作为启动类:需要引入@SpringBootApplication注解代表该类是启动类

⑤启动类的main方法里写(固定写法):

SpringApplication.run(Main.class,args);//自动创建ioc容器,启动tomcat服务器软件

⑥在启动类的包下创建和启动类类同等级的包controller(启动类会自动扫描该包)

⑦在controller包里创建HelloController类,该类需要引入@RestController、@RequestMapping("命名1")注解

⑧在HelloController类里写方法,方法要引入@GetMapping("命名2")注解

⑨运行启动类,查看端口号

⑩在网页输入“ localhost:端口号/命名1/命名2 ” 来访问写的方法的返回值


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用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、付费专栏及课程。

余额充值