我使用我的groupId和artifactId创建了一个非常简单的SpringBoot项目。看起来它不想开始,并且缺少某些映射。但是,当我使用与spring.io网站上的SPring Boot教程相同的软件包名称和类名时,它可以工作。
我的POM如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.spring</groupId>
<artifactId>firstspringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
我的资源“问候”
package com.my.spring.firstspringboot.entities;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class Greeting {
private String id;
private String firstName;
private String lastName;
public Greeting() {
}
public Greeting(String firstName, String lastName) throws NoSuchAlgorithmException {
this.setFirstName(firstName);
this.setLastName(lastName);
this.setId(new StringBuilder().append(SecureRandom.getInstance("SHA1PRNG").nextInt()).append("_").append(System.currentTimeMillis()).toString());
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return new StringBuilder().append("{\n\"name\":\"").append(this.firstName).append(" ").append(this.lastName).append("\"\n").append(",").append("{\n\"id\":\"").append(this.id).append("\"").toString();
}
}
及其控制器:
package com.my.spring.firstspringboot.controllers;
import java.security.NoSuchAlgorithmException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.my.spring.firstspringboot.entities.Greeting;
@RestController
public class GreetingController {
@RequestMapping("/")
public String home() {
return "Welcome!";
}
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(name="name",defaultValue="John") String name) throws NoSuchAlgorithmException {
return new Greeting(name,name);
}
}
和主应用
package com.my.spring.firstspringboot.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App
{
public static void main(String[] args)
{
SpringApplication.run(App.class, args);
}
}
在这里我还需要做些其他的事情吗?我以为它只是默认情况下的引导和自动配置。
答案 0 :(得分:0)
尝试在您的requestMapping中添加http方法
@RequestMapping(path = "/hello", method=RequestMethod.GET)
或尝试删除您使用路径“ /”声明的第一个端点
答案 1 :(得分:0)
根据文档(感谢@JBNizet指出),我需要将主应用程序保持在软件包名称com.my.spring.firstspringboot
原因在于,由于Spring是工件ID,因此Spring会立即在firstspringboot
子包下查找执行类。
正确的方法应该是:
1)将应用程序运行程序类放在比资源/控制器类更高的子包中。
例如com.my.firstapplication.App
,com.my.firstapplication.resources.Resource
等
2)如果我依靠手动设置,请使用@EnableAutoConfiguration
和@ComponentScan
3)如果我依靠Spring来做-是@SpringBootApplication