springboot输出hello world,3种方式(String,JSON,jsp),IDEA开发工具

本文详细介绍了如何使用Spring Initializr创建SpringBoot项目,并通过三种不同的方法实现RESTful API的响应,包括直接返回字符串、JSON对象及JSP页面。

新建项目:

File -> new -> Project -> Spring Initializr -> Next -> Next -> Next
-> Project name: demo
Project location: D:/project/demo
->Finish

 

编写时候,class自动导入包的引用
File -> Settings -> Editor -> General -> Auto Import ->
(Add unambiguous imports on the fly和Optimize imports on the fly 打钩) -> OK

 

1.String方式

在com.example.demo目录下新建HelloController  class文件

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello world"; } }

idea右上角,启动项目,http://localhost:8080/hello 访问

 

 

 

 

2.JSON方式

在com.example.demo目录下新建Hello  class文件

package com.example.demo;

public class Hello{
    private String name;

    public Hello() {}

    public Hello(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在com.example.demo目录下新建HelloController2 class文件

package com.example.demo;

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

@RestController
public class HelloController2 {
    
    @RequestMapping("/hello2")
    public Hello hello(){
        Hello hello = new Hello("hello world");
        /*
        或
        Hello hello = new Hello();
        hello.setName("hello world");
        */
        return hello;
    }
}

idea右上角,启动项目,http://localhost:8080/hello2 访问

 

 

 

 

3.jsp方式

pom.xml中增加

        <!-- jstl JSP标准标签库  -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- 返回jsp页面还需要这个依赖 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

pom.xml中有关artifactId找不到

1).file -> settings -> 搜索maven -> always update snapshots 打钩 -> OK
2).右下角选择 import...

 

resources   application.properties中增加

server.port=8080
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

src.main目录下新建webapp,webapp下新建WEB-INF,

WEB-INF下新建hello.jsp

 

在项目中鼠标右键new的时候无JSP
File -> Project structure -> Modules -> Web -> Web Resource Directories -> + 选择项目 -> OK

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    hello world
</body>
</html>

在com.example.demo目录下新建HelloController3 class文件

 
  
package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class HelloController3 {

@RequestMapping("hello3")
public ModelAndView hello(){
ModelAndView model = new ModelAndView();
model.setViewName("hello");
return model;
}
}

idea右上角,启动项目,http://localhost:8080/hello3 访问

转载于:https://www.cnblogs.com/songfei90/p/10647722.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值