0

I've got configuration like this (using Maven):

pom.xml

<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>springbootPlural</groupId> <artifactId>das-boot</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>das-boot</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> </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>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> </dependencies> </project> 

application.properties:

server.port=0 #(for random port) spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.datasource.url=jdbc:mysql://localhost:3306/mojabaza spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect User.class package com.test.testowa.model; import org.springframework.data.jpa.domain.AbstractPersistable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class User extends AbstractPersistable<Long> { private String userId; private String userName; private String password; public User() { } public User(String userId, String userName, String password) { this.userId = userId; this.userName = userName; this.password = password; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 

UserController.class

package com.test.testowa.controller; import com.test.testowa.Service.UserService; import com.test.testowa.model.User; 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.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { private UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } @RequestMapping("/delete/{id}") public String deleteUser(@PathVariable Long id) { return userService.deleteUser(id); } @RequestMapping("/add") public User addUser(User user) { return userService.addUser(user); } @RequestMapping("/list/{id}") public User findOne(@PathVariable Long id) { return userService.findOne(id); } @RequestMapping("/list") public List<User> userList() { return userService.userList(); } } 

UserServiceImpl.class

package com.test.testowa.Service.impl; import com.test.testowa.Service.UserService; import com.test.testowa.model.User; import com.test.testowa.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { private UserRepository userRepository; @Autowired public UserServiceImpl(UserRepository userRepository) { this.userRepository = userRepository; } @Override public List<User> userList() { return userRepository.findAll(); } @Override public User findOne(Long id) { return userRepository.findOne(id); } @Override public User addUser(User user) { return userRepository.save(user); } @Override public String deleteUser(Long id) { userRepository.delete(id); return "{'message':'User deleted'}"; } } 

UserRepository.class

package com.test.testowa.repository; import com.test.testowa.model.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { } 

UserService.class

package com.test.testowa.Service;

import com.test.testowa.model.User; import java.util.List; public interface UserService { List<User> userList(); User findOne(Long id); User addUser(User user); String deleteUser(Long id); } 

Hibernate creates table "user" flawlessly, but when I'm inserting some values in Postman e.g POST -> localhost:43441/shipwrecks/add, with application/json header and body:

{ "id2":1, "name":"name1", "description":"desc1" } 

And I'm getting: Hibernate: insert into shipwreck (description, id2, name) values (?, ?, ?) in console. When I'm inserting data in mysql manually everything go well. Also GET ->localhost:43563/shipwrecks/list works well. Did I make a mistake in this code? Maybe wrong version of Spring Boot?

2
  • are all columns in the row null or just some of them? Commented Mar 8, 2018 at 19:49
  • Only "id" gets incremented, other are nulled. Commented Mar 8, 2018 at 20:05

1 Answer 1

1

You need to specify the POST method for the add method in the Controller. I assume your User object is null and the data that you put in your request body does not reach your service.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.