不使用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、起透看视 启东麻将辅助软件价...
记者揭秘开挂插件!桃乐甘肃麻将... 记者揭秘开挂插件!桃乐甘肃麻将多功能开挂插件,桃乐甘肃麻将切实有开挂插件1、用户打开应用后不用登录就...
专业讨论辅助工具!抚顺麻将多功... 专业讨论辅助工具!抚顺麻将多功能辅助工具,抚顺麻将一直真的是有辅助工具抚顺麻将是不是有人用挂微扑克w...
重大消息透视器app!重庆蛮籽... 重大消息透视器app!重庆蛮籽麻将多功能透视器app,重庆蛮籽麻将切实真的是有透视器app1、许多玩...
我来教教大家辅助app!营口9... 您好,营口92麻将这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
发现一款辅助器工具!博乐温州棋... 发现一款辅助器工具!博乐温州棋牌多功能辅助器工具,博乐温州棋牌好像存在有辅助器工具1、许多玩家不知道...
玩家必备科技透视器脚本!奥云集... 玩家必备科技透视器脚本!奥云集安麻将多功能透视器脚本,奥云集安麻将都是是有透视器脚本1、奥云集安麻将...
终于知道辅助攻略!牌乐门多功能... 终于知道辅助攻略!牌乐门多功能辅助攻略,牌乐门确实是有辅助攻略1、许多玩家不知道牌乐门辅助怎么退出观...
必看攻略外挂方法!良人麻将多功... 您好,良人麻将这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款...