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

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

相关内容

热门资讯

透视透视挂!wepoker私人... 透视透视挂!wepoker私人局可以透视,wepoker好友助力码(竟然有挂)1、操作简单,无需注册...
透视中牌率!wpk有那种辅助吗... 透视中牌率!wpk有那种辅助吗,wpk俱乐部是真的吗,2025教程(原来真的有挂)1、wpk俱乐部是...
透视智能ai!wepoker透... 透视智能ai!wepoker透视是真的吗,wejoker辅助软件视频(一直真的是有挂)该软件可以轻松...
透视工具!wpk辅助,wpk真... 透视工具!wpk辅助,wpk真的有透视嘛,德州教程(好像存在有挂)1、金币登录送、破产送、升级送、活...
透视了解!wepokerplu... 透视了解!wepokerplus开挂,wepoker怎么买辅助(本来真的有挂);1、wepoker怎...
透视线上!wpk模拟器,如何下... 透视线上!wpk模拟器,如何下载wpk透视版,细节揭秘(都是是有挂);1、实时如何下载wpk透视版开...
透视教学!wepoker有机器... 透视教学!wepoker有机器人吗,wepoker俱乐部辅助器(一直有挂)1、下载好wepoker俱...
透视好牌!wpk模拟器多开,w... 透视好牌!wpk模拟器多开,wpk私人辅助,曝光教程(确实是有挂)1、进入游戏-大厅左侧-新手福利-...
透视了解!wepoker-h5... 透视了解!wepoker-h5下载,wepoker模拟器哪个(好像存在有挂);1、点击下载安装,we...
透视最新!wpk可以作弊吗,w... 透视最新!wpk可以作弊吗,wpk是真的还是假的,介绍教程(其实真的有挂);亲,关键说明,wpk是真...