不使用Spring Boot的Spring框架简单REST服务
创始人
2024-12-29 10:00:33
0

要创建一个简单的REST服务,不使用Spring Boot,可以按照以下步骤:

  1. 创建一个Maven项目,或者使用其他构建工具如Gradle。

  2. 在项目的pom.xml文件中添加Spring框架的依赖项。以下是常用的依赖项:


    
        org.springframework
        spring-core
        5.3.12.RELEASE
    
    
        org.springframework
        spring-web
        5.3.12.RELEASE
    

  1. 创建一个Controller类来处理REST请求。以下是一个简单的示例:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello/{name}")
    public ResponseEntity sayHello(@PathVariable String name) {
        String message = "Hello, " + name + "!";
        return new ResponseEntity<>(message, HttpStatus.OK);
    }
}
  1. 创建一个启动类,初始化Spring应用程序上下文并启动服务器。以下是一个简单的示例:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example")
@Import({HelloController.class})
public class Application {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        context.registerShutdownHook();
    }
}
  1. 创建一个Web配置类,用于配置Servlet和其他相关的Web组件。以下是一个简单的示例:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}
  1. 运行应用程序并访问REST端点。在浏览器或使用curl命令访问http://localhost:8080/api/hello/{name},其中{name}是您要问候的人的名称。

请注意,这只是一个简单的示例,仅包含必要的最小配置。如果需要更复杂的功能,您可能需要添加其他配置和依赖项。

相关内容

热门资讯

解密透视!aapoker ai... 您好,aapoker ai插件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
解密透视!拱趴大菠萝作弊方法,... 解密透视!拱趴大菠萝作弊方法,微信微乐跑得快游戏辅助脚本,方针教程(详细教程)-哔哩哔哩1)拱趴大菠...
了解透视!pokemomo辅助... 了解透视!pokemomo辅助软件,微乐河南家乡麻将辅助脚本,策略教程(有挂猫腻)-哔哩哔哩1、po...
普及透视!wepoker-h5... 普及透视!wepoker-h5下载,微乐如何自己建脚本,举措教程(有挂存在)-哔哩哔哩1、wepok...
揭露透视!epoker底牌透视... 揭露透视!epoker底牌透视,微乐辅助靠谱麻,资料教程(有挂透明挂)-哔哩哔哩1、玩家可以在epo...
总结透视!wepoker免费脚... 总结透视!wepoker免费脚本咨询,微乐自建房脚本如何下载,资料教程(有挂攻略)-哔哩哔哩1.we...
揭露透视!哈糖大菠萝可以开挂吗... 揭露透视!哈糖大菠萝可以开挂吗,微乐多乐跑作弊,大纲教程(有挂透视)-哔哩哔哩1、哈糖大菠萝可以开挂...
关于透视!wpk模拟器,微乐小... 关于透视!wpk模拟器,微乐小程序自建房透视,绝活儿教程(真实有挂)-哔哩哔哩暗藏猫腻,小编详细说明...
详情透视!wepoker私人局... 详情透视!wepoker私人局透视,微乐兰州麻将小程序辅助,讲义教程(有挂方略)-哔哩哔哩详情透视!...
解谜透视!来玩app破解,微乐... 解谜透视!来玩app破解,微乐小程序微乐家乡辅助器,阶段教程(真实有挂)-哔哩哔哩1、这是跨平台的来...