1

My issue is I get 404 error when calling the spring boot application on localhost:8080/users

package com.myproj.users.controller;

import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.security.Principal;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.myproj.users.Greeting;
import com.myproj.users.PhysicalCharacteristicsRepository;
import com.myproj.users.UserRepository;
import com.myproj.users.UserResource;

@RestController
@RequestMapping("/users")
public class UserRestController {

    private UserRepository userRepository;
    private PhysicalCharacteristicsRepository characteristicsRepository;

    @RequestMapping(value = "/greeting/", method = RequestMethod.GET)
    public String greeting() throws UserPrincipalNotFoundException {
        return "Greeting";
    }

    @RequestMapping(value = "/error/")
    public String error() {
        return "Error handling";
    }

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

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Greeting sayHello(@RequestParam(value = "name", required = false, defaultValue = "Stranger") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

    @Autowired
    UserRestController(UserRepository userRepository, PhysicalCharacteristicsRepository characteristicsRepository) {
        this.userRepository = userRepository;
        this.characteristicsRepository = characteristicsRepository;
    }
}


package com.myproj.users.controller;

import java.nio.file.attribute.UserPrincipalNotFoundException;

import org.springframework.hateoas.VndErrors;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.weather.exceptions.UserNotFoundException;

@ControllerAdvice
class UserControllerAdvice {

    @ResponseBody
    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    VndErrors userNotFoundExceptionHandler(UserNotFoundException ex) {
        return new VndErrors("error", ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(UserPrincipalNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    VndErrors userPrincipalNotFoundException(UserPrincipalNotFoundException ex) {
        return new VndErrors("error", ex.getMessage());
    }
}

package com.myproj.users;

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

}

I have tested the spring project in https://spring.io/guides/gs/actuator-service/ and it worked so I ignore what's going on.

Sofiane
  • 908
  • 4
  • 19
  • 45

2 Answers2

0

I have defined a controller to manage errors. I have copied it from Spring Boot Remove Whitelabel Error Page The new Application class is the following :

package com.test;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@ComponentScan(basePackages = "com.test")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "com.test")
@EntityScan(basePackages = "com.test")
public class Application {

    static final Logger logger = LogManager.getLogger(Application.class.getName());

    public static void main(String[] args) {
        logger.debug("Entered the application");
        SpringApplication.run(Application.class, args);
    }

    private Application() {
    }
}

As you can see I have added a controller in ComponentScan as follows :

 @ComponentScan(basePackages = "com.test")



@EnableJpaRepositories(basePackages = "com.test")
@EntityScan(basePackages = "com.test")

To test I used curl curl http://localhost:9002/eleves/Hammami/ and firefox.

Community
  • 1
  • 1
Sofiane
  • 908
  • 4
  • 19
  • 45
0

Changing @Controller to @RestController solved my issue.

In my case I was using thymeleaf(MVC), after that I switched to pure backend.

Paul
  • 1,344
  • 4
  • 19
  • 37