使用Java+SpringBoot写一个简单的后端接口

打开idea,新建项目,选择Spring Initializr,自定义工件名称和路径,选择项目SDK和Java版本,点击下一步

 选择Spring Web,完成

 

 项目创建成功

 

 在src/main/java/com/example/demo路径下创建软件包cotroller,在内部创建Java类DemoController

 在DemoController类下编写代码:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @GetMapping("/hello")
    public String hello(){
        return "hello world ";
    }
}

RestController注解可以使前端访问到后端,表示请求数据;

Controller表示请求页面或数据

GetMapping注解表明前端发送get请求,

"/hello"表示地址,在浏览器输入localhost:8080/hello127.0.0.1:8080/hello 即可访问到; 

也可以使用 @RequestMapping注解,value参数表示路由路径,method表示请求方法,例:

@RequestMapping(value = "/hello1",method = RequestMethod.GET)
    public String hello1(){return "hello worlds";}

在后端修改代码需要重启项目才能在前端生效,如果希望不重启项目也能实时生效可参考新版IDEA Springboot项目热部署_洛潆的博客-CSDN博客

 POST请求可以使用@PostMapping注解;

POST请求无法通过浏览器地址栏查看效果,可以使用swagger查看效果;

swagger可以自动生成接口文档,并且带有调试功能;配置方法参考:Spring Boot项目swagger配置_洛潆的博客-CSDN博客

@PostMapping("/hello6")
    public String hello6(String name){return "hello worlds"+name;}

 

好的,这里是一个简单Spring Boot登陆页面的实现示例: 首先,你需要在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 接下来,在你的应用程序中创建一个WebSecurityConfig类,用于配置安全性和身份验证: ```java @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/", "/home").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("password").roles("USER"); } } ``` 这个例子中,我们简单地创建了一个基本的登陆页面,并且只有一个用户“user”可以登录。signin()函数将返回"login"这个字符串告诉springboot使用spring-security提供的默认登录页面。 最后,我们需要创建一个控制器来处理我们的请求: ```java @Controller public class LoginController { @RequestMapping(value="/login", method=RequestMethod.GET) public String login(Model model, String error, String logout) { if (error != null) model.addAttribute("error", "Your username and password is invalid."); if (logout != null) model.addAttribute("message", "You have been logged out successfully."); return "login"; } } ``` 我们用@RequestMapping注解告诉Spring,访问/login映射到login()方法,返回我们的登陆页面。 现在你可以在你的浏览器中访问localhost:8080/login来测试这个简单的登陆页面了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛潆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值