Skip to content
Advertisement

Not able to update table contents through postman using Spring Boot

package com.shashank.topic;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Topic {

@Id
private String id;
private String name;
private String description;

public Topic() {
}

public Topic(String id, String name, String description) {
    super();
    this.id = id;
    this.name = name;
    this.description = description;
}

public String getId() {
    return id;
}

public void setId(String id) {

    this.id = id;
}

public String getName() {

    return name;
}

public void setName(String name) {

    this.name = name;
}

public String getDescription() {

    return description;
}

public void setDescription(String description) {

    this.description = description;
}
}

Repository is configured like this

package com.shashank.topic;

import org.springframework.data.repository.CrudRepository;

public interface TopicRepository extends CrudRepository<Topic, String> {



/***
 * CURD OPERATIONS
 * getAllTopics()
 * getTopic(String id)
 * updateTopic(Topic topic)
 * deleteTopic(String id)
 * */

 }

This is another class

package com.shashank.topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class TopicService {

@Autowired
private TopicRepository topicRepository;

public List<Topic> getAllTopics() {

    List<Topic> topics = new ArrayList<>();
    topicRepository.findAll().forEach(topics::add);
    return topics;
}

public Optional<Topic> getTopic(String id) {

    return topicRepository.findById(id);
}

public void addTopic(Topic topic) {
    topicRepository.save(topic);
}

public void updateTopic(String id, Topic topic) {
    topicRepository.save(topic);
}

public void deleteTopic(String id) {

    topicRepository.deleteById(id);
}
}

I’m able to post the topics in my table and get as well from the postman but when I try to use put, to update the contents of the table, nothing changes.

I was trying to put the below command to update the table

{
    "id": "javascript",
    "name": "Update javascript",
    "description": "Update javascript Description"
}

where “javascript” was already in the table, with name as “javascript” and description as “javascript description”

Here is my spring console log

2021-03-03 17:33:05.625  INFO 9848 --- [           main] 
com.shashank.CourseApiDataApplication    : Starting 
CourseApiDataApplication using Java 13.0.2 on DESKTOP-FGT8D3V with PID 9848 
(D:MavenProjectscourse-api-datatargetclasses started by shash in 
D:MavenProjectscourse-api-data)
2021-03-03 17:33:05.628  INFO 9848 --- [           main] 
com.shashank.CourseApiDataApplication    : No active profile set, falling 
back to default profiles: default
2021-03-03 17:33:06.262  INFO 9848 --- [           main] 
.s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA 
repositories in DEFAULT mode.
2021-03-03 17:33:06.290  INFO 9848 --- [           main] 
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository 
scanning in 23 ms. Found 1 JPA repository interfaces.
2021-03-03 17:33:06.618  INFO 9848 --- [           main] 
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 
8080 (http)
2021-03-03 17:33:06.623  INFO 9848 --- [           main] 
o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-03 17:33:06.624  INFO 9848 --- [           main] 
org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache 
Tomcat/9.0.43]
2021-03-03 17:33:06.720  INFO 9848 --- [           main] o.a.c.c.C.[Tomcat]. 
[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-03 17:33:06.720  INFO 9848 --- [           main] 
w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: 
initialization completed in 1039 ms
2021-03-03 17:33:06.876  INFO 9848 --- [           main] 
o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing 
PersistenceUnitInfo [name: default]
2021-03-03 17:33:06.901  INFO 9848 --- [           main] 
org.hibernate.Version                    : HHH000412: Hibernate ORM core 
version 5.4.28.Final
2021-03-03 17:33:06.964  INFO 9848 --- [           main] 
o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons 
Annotations {5.1.2.Final}
2021-03-03 17:33:07.016  INFO 9848 --- [           main] 
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-03-03 17:33:07.018  WARN 9848 --- [           main] 
com.zaxxer.hikari.util.DriverDataSource  : Registered driver with 
driverClassName=org.apache.derby.jdbc.EmbeddedDriver was not found, trying 
direct instantiation.
2021-03-03 17:33:07.269  INFO 9848 --- [           main] 
com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Driver does not 
support get/set network timeout for connections. (Feature not implemented: No 
details.)
2021-03-03 17:33:07.271  INFO 9848 --- [           main] 
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-03-03 17:33:07.281  INFO 9848 --- [           main] 
org.hibernate.dialect.Dialect            : HHH000400: Using dialect: 
org.hibernate.dialect.DerbyTenSevenDialect
2021-03-03 17:33:07.911  INFO 9848 --- [           main] 
o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform 
implementation: 
[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-03-03 17:33:07.916  INFO 9848 --- [           main] 
j.LocalContainerEntityManagerFactoryBean : Initialized JPA 
EntityManagerFactory for persistence unit 'default'
2021-03-03 17:33:08.134  WARN 9848 --- [           main] 
JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled 
by default. Therefore, database queries may be performed during view 
rendering. Explicitly configure spring.jpa.open-in-view to disable this 
warning
2021-03-03 17:33:08.220  INFO 9848 --- [           main] 
o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 
'applicationTaskExecutor'
2021-03-03 17:33:08.460  INFO 9848 --- [           main] 
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 
(http) with context path ''
2021-03-03 17:33:08.469  INFO 9848 --- [           main] 
com.shashank.CourseApiDataApplication    : Started CourseApiDataApplication 
in 3.207 seconds (JVM running for 3.973)

Advertisement

Answer

There is something unusual with your Id. Why did you consider Id as String? It should be Long or int, So maybe your id is your main problem.

Also, It would be better that put the controller to help us to debug better. Anyway, there are some ideas.

public void updateTopic(String id, Topic topic) {
    Topic myTopic = topicRepository.findById(id);
    topicRepository.save(myTopic);
}

  • Controller
@PutMapping("/topics")  
private Topic update(@RequestBody Topic topic){  
topicService.updateTopic(topic);  
return topic;  
}  

  • Strongly recommend you to change your ID from string to int or Long and try the above solution.
Advertisement