I'm getting an error where the entity class's variable is not found while the variable is clearly there and the entity class is found
@RestController
public class LanguageController {
@Autowired
private LanguageService service;
@RequestMapping("/language")
public List<Language> allLanguages() {
return service.getAllLanguages();
}
@RequestMapping("/language/{id}")
public Optional<Language> allLanguagesById(@PathVariable String id) {
return service.getLanguagesById(id);
}
@RequestMapping(value = "/language", method = RequestMethod.POST)
public void addTopic(@RequestBody Language topic) {
service.addLanguage(topic);
}
// addLanguage used to save/add depending if obj already exists
// Will need more variable in obj for it to truly update
@RequestMapping(value = "/language/{id}", method = RequestMethod.PUT)
public void updateTopic(@RequestBody Language lang) {
service.addLanguage(lang);
}
@RequestMapping(value = "/language/{id}", method = RequestMethod.DELETE)
public void deleteTopics(@PathVariable String id) {
service.deleteLanguage(id);
}
}
entity class
package com.Alex.language;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import com.Alex.language.Language;
// JPA to create columns according it
// Act as table labels
@Entity
public class Topics {
// Mark primary key
@Id
private String id;
private String name;
private String description;
// Many topics to one language
@ManyToOne
private Language lang;
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;
}
public Topics() {
}
public Topics(String id, String name, String description, String langId) {
super();
this.id = id;
this.name = name;
this.description = description;
this.lang = new Language(langId);
}
public Language getLang() {
return lang;
}
public void setLang(Language lang) {
this.lang = lang;
}
}
package com.Alex.language;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Language {
@Id
private String langId;
public String getLangId() {
return langId;
}
public void setLangId(String langId) {
this.langId = langId;
}
public Language(String langId) {
super();
this.langId = langId;
}
public Language() {
}
}
Controller class
package com.Alex.language;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.Alex.language.Language;
// Used to create RESTful web services
@RestController
public class TopicsController {
// As the database is embedded, will have to add data before able to see in Postman
@Autowired
private TopicService service;
@RequestMapping("/language/{id}/topics")
public List<Topics> getAllTopics(@PathVariable String id) {
return service.getAllTopicsByLanguageId(id);
}
// @PathVariable get variable from @RequestMapping {id}
@RequestMapping("/language/{langId}/topics/{id}")
public Optional<Topics> getSpecifiedTopic(@PathVariable String id) {
return service.getTopic(id);
}
// @RequestBody to convert JSON object to Java object to be used
@RequestMapping(value = "/language/{langId}/topics", method = RequestMethod.POST)
public void addTopic(@RequestBody Topics topic, @PathVariable String langId) {
topic.setLang(new Language(langId));
service.addTopic(topic);
}
@RequestMapping(value = "/language/{langId}/topics/{id}", method = RequestMethod.PUT)
public void updateTopic(@PathVariable String langId, @RequestBody Topics topic) {
topic.setLang(new Language(langId));
service.updateTopic(topic);
}
@RequestMapping(value = "/language/{langId}/topics/{id}", method = RequestMethod.DELETE)
public void deleteTopics(@PathVariable String id) {
service.deleteTopic(id);
}
}
So, I get the following error message when I type in the url something like this in Postman. localhost:8080/language/java/topics ( POST )
message=Unable to find com.Alex.language.Language with id langId; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.Alex.language.Language with id langId, path=/language/java/topics}]
EDIT: Remove " " between langId which makes a literal String. Silly me, this is one of the mistakes I make most commonly :/
Solving that, I'm getting " Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'langId' in value "/language/${langId}/topics/{id}". I believe there is some configuration problem in main or so.