不使用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}是您要问候的人的名称。

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

相关内容

热门资讯

四分钟了解!哈局十三张,大赢家... 四分钟了解!哈局十三张,大赢家跑得快辅助,分享教程(有挂脚本)1、大赢家跑得快辅助系统规律教程、大赢...
8分钟了解!星星武汉麻将胡牌技... 8分钟了解!星星武汉麻将胡牌技巧,赣牌圈开挂是真的吗,科技教程(有挂揭秘)1、进入游戏-大厅左侧-新...
4分钟了解!菠萝德州app有挂... 4分钟了解!菠萝德州app有挂吗,新玉海楼茶苑吗,必胜教程(有挂神器)1、在菠萝德州app有挂吗ai...
6分钟了解!微友麻将,浙江游戏... 6分钟了解!微友麻将,浙江游戏大厅有猫腻吗,透视教程(有挂解说)亲,关键说明,浙江游戏大厅有猫腻吗赛...
一分钟了解!斗棋红中胡牌有没有... 一分钟了解!斗棋红中胡牌有没有什么规律,开心十三张有没有挂,2025版教程(有挂技巧);暗藏猫腻,小...
2分钟了解!琼崖海南麻将怎么提... 2分钟了解!琼崖海南麻将怎么提高胜率,福建天天开心王国辅助器,揭秘教程(有挂工具)一、琼崖海南麻将怎...
八分钟了解!乐乐游戏辅助器,众... 八分钟了解!乐乐游戏辅助器,众乐联盟有挂吗,可靠教程(有挂透视)1、全新机制【众乐联盟有挂吗软件透明...
一分钟了解!雀神麻将辅牌器购买... 一分钟了解!雀神麻将辅牌器购买,微信随意玩9人拼三张辅助器,实用技巧(有挂秘籍)1、用户打开应用后不...
二分钟了解!新华棋牌有没有挂,... 二分钟了解!新华棋牌有没有挂,打小闲川南棋牌为什么总是输,揭秘教程(有挂软件)暗藏猫腻,小编详细说明...
三分钟了解!汇友手游外 挂,边... 三分钟了解!汇友手游外 挂,边锋干瞪眼外挂效果,必备教程(有挂软件)进入游戏-大厅左侧-新手福利-激...