在Spring Boot项目中,应该将RestController放在哪里。

huangapple go评论58阅读模式
英文:

where should I put RestController in spring boot project

问题

我使用了IDEA IDE来开发一个Java Spring Boot项目,按照教程进行操作。
然而,在教程中有一个如下所示的函数。我不知道在我的Spring Boot项目中应该把这个函数放在哪里。
这个函数是:

package com.helloworld.controller;

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

/**
 * Created by fangxiao on 2017/3/24.
 */
@RestController
public class Controller {

    @RequestMapping("/helloworld")
    public String helloWorld() {
        return "helloworld";
    }
}

根据下面的项目结构,我应该把这个函数放在哪里?
项目结构

英文:

I used idea IDE to developed a Java springboot project follow a tutorial.
However, in the tutorial, there's a function shown below. I do not know where to put this function in me spring boot project.
The function:

package com.helloworld.controller;

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

/**
 * Created by fangxiao on 2017/3/24.
 */
@RestController
public class Controller {

    @RequestMapping("/helloworld")
    public String helloWorld() {
        return "helloworld";
    }
}

where should I put this function as for the below project structure?<br/>
project structure

答案1

得分: 1

根据您的示例,Controller类的组织结构如下图所示:

在Spring Boot项目中,应该将RestController放在哪里。

我还创建了其他在spring-boot项目中经常使用的包。

英文:

Following your example, the Controller Class have a organization like this picture:

在Spring Boot项目中,应该将RestController放在哪里。

I also created another packages that are frequentelly used in a spring-boot project

答案2

得分: 0

由于您的包是:

package com.helloworld.controller;

您可以在java文件夹内创建文件夹结构,如com -> helloworld -> controller,并将代码(Controller.java类)放在其中。

英文:

Since your package is :

package com.helloworld.controller;

You can create folder structure inside java folder com -> helloworld -> controller and keep the code (Controller.java class) inside.

答案3

得分: 0

src/main/java下创建一个名为com.abc.rest的包,并创建一个RestController类:

以下是示例代码,要访问此API,请使用类似于:localhost:8080/test/headers 的URL。

@RestController
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class Rest {
    
    @GetMapping("/headers")
    public String listAllHeaders(@RequestHeader Map<String, String> headers) {
        headers.forEach((key, value) -> {
            System.out.println(String.format("Header '%s' = %s", key, value));
        });
        return "success";
    }
}
英文:

Create a package under src/main/java like com.abc.rest and create RestController class
:Here is the sample code and to access this api use url like : localhost:8080/test/headers

@RestController
@RequestMapping(value = &quot;/test&quot;, produces = MediaType.APPLICATION_JSON_VALUE)
public class Rest {
	
	@GetMapping(&quot;/headers&quot;)
	public String listAllHeaders(@RequestHeader Map&lt;String, String&gt; headers) {
	    headers.forEach((key, value) -&gt; {
	        System.out.println(String.format(&quot;Header &#39;%s&#39; = %s&quot;, key, value));
	    });
	 return &quot;success&quot;;
	}
}

答案4

得分: 0

出于学习目的,手动操作是有益的,但如果你想跳过这一步,你可以在 src/main/java 中创建如上所示的类,IntelliJ 将会检测并提供将其重新定位到正确的包的选项。

英文:

For learning purpose is good to do it manually, but if you want to skip it, you can create the class shown above in the src/main/java and IntelliJ will dectect and offer to relocate it to the proper package.

答案5

得分: 0

创建名为 com.project.controller 的包,位于以下目录:

D:\Bill\java\test\src\main\java

英文:

Create your package com.project.controller under

> D:\Bill\java\test\src\main\java

directory

huangapple
  • 本文由 发表于 2020年8月22日 10:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63532151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定