栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用spring boot开发基于rest的服务的两种方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用spring boot开发基于rest的服务的两种方式

maven依赖


    4.0.0
    
    
    
    
    
    
    com.esoon.eda
    eda20110510
    pom
    0.0.1-SNAPSHOT
    
        eda-admin
    
    eda20110510
    Demo project for Spring Boot
    
        1.8
    

    
    
        

            
            
                org.springframework.boot
                spring-boot-dependencies
                2.5.13
                pom
                import
            
        
    


    
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


第一种方式:使用@EnableAutoConfiguration和@RestController注解 主程序:App.java
package com.esoon.eda.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableAutoConfiguration
@RestController
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}
浏览器访问

注解说明: 

 @RestController说明直接返回内容到页面,不再进入视图解析

第二种方式:@SpringBootApplication和@RestController 主程序:EdaApplication.java
package com.esoon.eda.admin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class EdaApplication {
    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(EdaApplication.class, args);
    }

    @GetMapping("/burns")
    public String hello(){
        return "burns";
    }
}
浏览器访问

相关注解说明 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/894968.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号