Ошибка 404 при попытке запустить пример REST Helloworld отсюда https://spring.io/guides/gs/rest-service/ - PullRequest
0 голосов
/ 28 апреля 2020

Я добавил классы / компоненты в соответствии с инструкциями из кода в теме ссылки, однако, когда я пытаюсь запустить его с использованием сервера Tomcat 9, я получаю «его ошибка (HTTP 404 Not Found) означает, что это Программа смогла подключиться к веб-сайту, но нужная страница не была найдена. Возможно, веб-страница временно недоступна. Кроме того, веб-сайт мог изменить или удалить веб-страницу. "

это 4 класса, которые у меня есть


1) GreetingController. java

package com.example.HelloRestService;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));

}
}

2) HelloRestServiceApplication. java

package com.example.HelloRestService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
//@ComponentScan(basePackages="com.example.HelloRestService")
public class HelloRestServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloRestServiceApplication.class, args);
    }

}

3) ServletInitilizer. java

package com.example.HelloRestService;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HelloRestServiceApplication.class);
    }

}

4) Приветствие. java

package com.example.HelloRestService;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

}

1 ) Я попытался переместить контроллер приветствия и приветствия в другой пакет - все еще нет go.

2) Я попытался добавить //@ComponentScan(basePackages="com.example.HelloRestService ") в файл" HelloRestServiceAppliation. java ", все еще нет go.

3) Я попытался изменить @GetMapping ("/ приветствие") на @GetMapping ("HelloRestService / приветствие") - по-прежнему нет go.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...