HTTP-статус 404 - не найден - Spring MVC и Hibernate - PullRequest
0 голосов
/ 13 января 2020

Я пытаюсь запустить свое приложение без использования сети. xml. Ниже приведен мой код для webAppInitalizer


import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppIntializer extends AbstractAnnotationConfigDispatcherServletInitializer 
{

    @Override
    protected Class<?>[] getRootConfigClasses() 
    {
        System.out.println("getRootConfigClasses");
        return new Class[]{AppConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() 
    {
        System.out.println("getServletConfigClasses");
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() 
    {
        System.out.println("getServletMappings");
        return new String[]{"/user/*"};
    }

}

Я вижу, что все мои методы работают. Приложение соединяется с базой данных из класса AppConfig, оно просто из WebConfig, оно ничего не делает. Любое предложение по этому вопросу будет очень полезно для меня. Ниже мой класс WebConfig


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.restAPI.test.controller"})
public class WebConfig  implements WebMvcConfigurer
{

}

Применение операции crud к классу контроллера ниже:


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.restAPI.test.model.UserInfo;
import com.restAPI.test.service.UserInfoService;

@RestController
public class UserInfoController 
{
    @Autowired
    private UserInfoService userInforService;

    // add a new user 
    @PostMapping("/user/add")
    public ResponseEntity<?> addUser(@RequestBody UserInfo user)
    {
        // make the id as long in future
        long id = userInforService.addUser(user);
        return ResponseEntity.ok().body("User has been added with userId: "+id);
    }

    // get user by id
    @GetMapping("/user/{id}")
    public ResponseEntity<?> getUserInfo(@PathVariable("id") int id)
    {
        System.out.println("in getUserInfo method");
        UserInfo user = userInforService.getUserInfo(id);
        return ResponseEntity.ok().body(user);
    }


    // get all the users
    @GetMapping("/user/allUsers")
    public ResponseEntity<?> getAllUsers(List<UserInfo> users)
    {
        List<UserInfo> users_list = userInforService.getAllUsers();
        return ResponseEntity.ok().body(users_list);
    }

    // update the user information
    @PutMapping("/user/update/id")
    public ResponseEntity<?> updateUserInfo(@PathVariable("id") int id, UserInfo user)
    {
        userInforService.updateUserInfo(id, user);
        return ResponseEntity.ok().body("User Information has been updated");
    }

    // delete the user information
        @DeleteMapping("/user/delete/id")
        public ResponseEntity<?> deleteUserInfo(@PathVariable("id") int id)
        {
            userInforService.deleteUserInfo(id);
            return ResponseEntity.ok().body("User Information has been deleted");
        }



}

Проблема в том, что я не получаю никакой ошибки в выводе. Я запускаю этот проект на сервере (Tomcat Server - версия 9) из Eclipse.

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