0

I have a Spring Boot Project with pom.xml like this

<groupId>com.project</groupId>
    <artifactId>prj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>prj</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

The Application class is like this

@SpringBootApplication
public class PrjApplication {

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

And I have a RestController where I am trying to do some CRUD Operations like this

@RestController("/register")
public class RegisterController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/user/{id}")
    public User getUser(@Valid @RequestBody Long id){
        User user = userRepository.getOne(id);
        return user;
    }

    @PostMapping("/new")
    public User saveUser(@Valid @RequestBody String name,@Valid @RequestBody String email,@Valid @RequestBody String password,@Valid @RequestBody String phone){
        System.out.println("savginv user");
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setName(name);
        user.setPhone(phone);
        userRepository.save(user);
        return user;
    }
}

I have made an Entity class; User like this

@Entity
@Table(name = "tbl_users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    String name, email, password, phone;

    public Long getId() {
        return id;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

When I try to save a user by calling the post method using PostMan it gives me 404 not found error.

{
    "timestamp": "2018-06-23T06:47:14.086+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/prj/register/new"
}

I am using JPARepository to perform Database operations.

6
  • 1
    Are you able to print this System.out.println("savginv user"); ?? Commented Jun 23, 2018 at 6:56
  • what is project context path? Commented Jun 23, 2018 at 6:58
  • No, the log is not printing, that means the method is not being called, right, so what should I do Commented Jun 23, 2018 at 6:58
  • How are you accessing it then?? update your url Commented Jun 23, 2018 at 6:59
  • 1
    Your url should be register/new only, remove 'prj` from it. Commented Jun 23, 2018 at 7:02

1 Answer 1

2

/prj isn't part of your controller. Either use a POST request without it (i.e., /register/new), or add it to your controller's mapping:

@RestController("/prj/register")
public class RegisterController {
    // rest of the code (no pun intended)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.