不使用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、游戏颠覆性的策略玩法,独...
热点讨论!wepoker内置辅... 热点讨论!wepoker内置辅助器,边锋老友威海荣成辅助,详细工具(有挂总结)1、游戏颠覆性的策略玩...
我来教大家!青橙竞技辅助器,丰... 我来教大家!青橙竞技辅助器,丰城双剑辅助,详细神器(有挂解密)1、青橙竞技辅助器有没有辅助教程、青橙...
记者发布!蜀山四川辅助脚本,来... 记者发布!蜀山四川辅助脚本,来物局潮汕辅助,详细技巧(有挂实锤)1、记者发布!蜀山四川辅助脚本,来物...
技术分享!途游辅助器软件,好友... 技术分享!途游辅助器软件,好友赣南新版本脚本,详细挂(有挂方法)1)好友赣南新版本脚本免费钻石:进一...
每日必看!闽游app破解,内蒙... 每日必看!闽游app破解,内蒙麻将外卦神器下载,详细app(讲解有挂)在进入内蒙麻将外卦神器下载软件...
2分钟细说!微信牵手跑辅助下载... 2分钟细说!微信牵手跑辅助下载,哥哥打大a脚本,详细软件(有挂辅助)1、下载好微信牵手跑辅助下载正确...