简体   繁体   中英

Springboot doesn't work except when hello is typed

I created a very simply SpringBoot project using my groupId and artifactId. Looks like it doesn't want to kick off and some mappings are missing. But when I use the same package names and classnames as the SPring Boot tutorial which is on spring.io site, it works.

My POM is the following:

<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>

My Resource 'Greeting'

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();
    }
}

And its Controller:

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);
    }

}

And main App

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);

    }

}

Is there something more that I need to do here? I thought it's simply a boot and autoconfigure by default.

Try to add http method in your requestMapping

@RequestMapping(path = "/hello", method=RequestMethod.GET)

or try removing first endpoint which you have declared with path "/"

According to the documentation (Thanks to @JBNizet for pointing that out), I needed to keep my main application under the package name com.my.spring.firstspringboot

The reason is that Spring looks immediately under the firstspringboot subpackage to find the execution class since it's the artifact ID.

The correct approach should probably be the following:

1) Keep the app runner class in higher subpackage than resource/controller classes.

eg com.my.firstapplication.App , com.my.firstapplication.resources.Resource etc.

2) If I rely on manual settings, use @EnableAutoConfiguration and @ComponentScan

3) If I am relying on Spring to do it - it's @SpringBootApplication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM