英文:
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项目中经常使用的包。
英文:
Following your example, the Controller Class have a organization like this picture:
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 = "/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";
}
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论