springBoot入门案例

SpringBoot作为Spring技术栈的一部分,提供快速构建Spring项目的脚手架,减少XML配置,聚焦业务。其特点包括快速整合第三方框架、开箱即用、内置Tomcat等。本文将指导你从创建maven项目、添加依赖、编写启动类到实现简单的Controller,通过http://localhost:8080/hello进行测试。
摘要由CSDN通过智能技术生成

SpringBoot是Spring技术栈的一个子工程,和我们熟知的Springframework同属于Spring的产品。

SpringBoot可以理解为我们搭建程序的脚手架,其主要的作用就是可以帮助我们快速的构建Spring项目,并且尽可能的减少一切xml配置,可以做到开箱即用,迅速上手,让我们可以更加专注业务而非配置。

springBoot的好处及特点

1、可以很快速的帮助我们快速整合(第三方框架),完全采用注解化的配置。

2、开箱即用(启动器starter-其实就是SpringBoot提供的一个jar包),但我们可以自己设置参数(.properties),即可快速摆脱这个方式。

3、springBoot提供了一些大型项目中常见的非功能性特性,如如安全、指标,健康检测、外部配置 内置Tomcat和Jetty容器

创建第一个SpringBoot项目

1、首先创建一个maven项目

2、创建完maven项目后在pom文件加入对应的依赖包

<!--添加父工程坐标 对各种常用依赖(并非全部)的版本进行了管理 可以理解为我们的之前的聚合的父pom-->
    <parent>   
        <groupId>org.springframework.boot</groupId>   
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

     <dependencies>
     <!--添加web启动器 SpringBoot提供的自动配置依赖 我们称为启动器-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3、配置完成后,我们需要去写一个启动类,去启动我们的springBoot

4、写一个controller就可以进行测试了

 

然后访问 http://localhost:8080/hello 就会出现

hello SpringBoot

就是这么简单。。。

下面是一个使用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、付费专栏及课程。

余额充值