I'm try to connect java spring boot with mysql. when i run the code i got the map address like this
This is my first code
This is EmpController1
package com.example.rest.controller;
import com.example.rest.repository.R1pro;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.rest.repository.EmpRepository1;
import java.util.ArrayList;
import java.util.List;
#RestController
#RequestMapping(value = "/emp")
#Slf4j
public class EmpController1 {
#Autowired
private EmpRepository1 empRepository1;
#RequestMapping(value="/ee", method = RequestMethod.GET)
#ResponseBody
public String getCategoryList() {
List<String> sj = new ArrayList<String>();
Gson gson= new Gson();
System.out.println(123);
List<R1pro> emps1 = this.empRepository1.findByLimit();
return emps1.toString();
}
}
This is my EmpRepository1 code
package com.example.rest.repository;
import com.example.rest.Emp;
import com.example.rest.Emp1;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
#Repository
public interface EmpRepository1 extends JpaRepository<Emp1, Integer> {
#Query(value = "select * from d_RANGE limit 1",nativeQuery = true)
public List<R1pro> findByLimit();
}
This is R1pro code
package com.example.rest.repository;
import java.util.Date;
public interface R1pro {
public String USER_ID();
public String CUST_GP();
}
This is my Emp1 code
package com.example.rest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;
#Entity
#Table(name = "TEMP_TEST_M78_2W")
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Emp1 {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private String USER_ID;
private String CUST_GP;
}
This is my Application code
package com.example.rest;
import com.example.rest.repository.EmpRepository;
import com.example.rest.repository.EmpRepository1;
import com.example.rest.repository.R1pro;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.List;
#SpringBootApplication
#Slf4j
public class Application {
#Autowired
EmpRepository empRepository;
#Autowired
EmpRepository1 empRepository1;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
CommandLineRunner start() {
return args -> {mysql();};
}
private void mysql() {
List<R1pro> emp1 = this.empRepository1.findByLimit();
}
}
When i run the code i got this result
[org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap#27ae5583]
So i change the EmpController1 code
package com.example.rest.controller;
import com.example.rest.repository.R1pro;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.rest.repository.EmpRepository1;
import java.util.ArrayList;
import java.util.List;
#RestController
#RequestMapping(value = "/emp")
#Slf4j
public class EmpController1 {
#Autowired
private EmpRepository1 empRepository1;
#RequestMapping(value="/ee", method = RequestMethod.GET)
#ResponseBody
public String getCategoryList() {
List<String> sj = new ArrayList<String>();
Gson gson= new Gson();
System.out.println(123);
List<R1pro> emps1 = this.empRepository1.findByLimit();
for (int i =0; i<emps1.size();i++)
{
sj.add(emps1.get(i).USER_ID()+" "+ emps1.get(i).CUST_GP());
}
return sj.toString();
}
}
When i run the code i got this Error message
java.lang.IllegalArgumentException: Invoked method public abstract java.lang.String com.example.rest.repository.R1pro.USER_ID() is no accessor method!
Actually this method same as mysql data columns
USER_ID varchar(150)
CUST_GP varchar(1)
This is my sql columns informations
I don't know what is the problem also any solution.. so if someone knows that please teach me
I really admire to solve this issue
thank you!
You need to tweak your interface into something like this -
public interface R1pro {
String getUSER_ID();
String getCUST_GP();
}
The resultset is mapped only to the getter methods similar to the one created in Emp1 class
Related
I'm trying to update the tagline of a movie node
This is my Movie class without the boilerplate section
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
#Node
public class Movie {
#Id
public String title;
public String tagline;
}
This is my movie repository. I'm including also all the imports just to be sure that I'm passing as much info as possible to hum might answer this question
package com.prompto.api4.movies;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import java.util.List;
interface MovieRepository extends Neo4jRepository<Movie, String> {
}
And this is how I try to create/update a specific movie. But sadly this approach only works fro create.
package com.prompto.api4.movies;
import org.neo4j.driver.*;
import org.neo4j.driver.types.TypeSystem;
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
#Service
public class MovieService {
private final MovieRepository movieRepository;
private final Neo4jClient neo4jClient;
private final Driver driver;
private final DatabaseSelectionProvider databaseSelectionProvider;
MovieService(MovieRepository movieRepository,
Neo4jClient neo4jClient,
Driver driver,
DatabaseSelectionProvider databaseSelectionProvider) {
this.movieRepository = movieRepository;
this.neo4jClient = neo4jClient;
this.driver = driver;
this.databaseSelectionProvider = databaseSelectionProvider;
}
#Transactional
public void createUpdateMovie(Movie movieValues){
Optional<Movie> theMovie = movieRepository.findById(movieValues.title);
if (theMovie.isPresent()){
theMovie.get().setTagline(movieValues.tagline);
}else {
movieRepository.save(movieValues);
}
}
}
The creating part is working but the update part does not.
How do I update the movie tagline?
I am working in a project to learn spring boot, i have a problem where i have a attribute actor_id stored in mongodb with a value x but when i do mongoRepository.findall(), he changes the value of my actor_id to the value of the object_id generated automatically by mongodb, so even if i want to find a value by id i have to put the object_id value instead of the value of actor_id stored in database. Image below to better help understand.
I wanted that the returned value of a get http://localhost:8093/actors in actor_id be = 1 instead of 61634ad37e775d4b87635129.
Below is my code.
Actor.java:
package com.film.SpringAplication.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
#Document(collection="actor")
#AllArgsConstructor
#Data
public class Actor {
#Id
String actor_id;
String first_name;
String last_name;
}
ActorController.java
package com.film.SpringAplication.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.film.SpringAplication.model.Actor;
import com.film.SpringAplication.service.ActorService;
#RestController
#RequestMapping("/actors")
public class ActorController {
#Autowired
ActorService actorService;
#PostMapping
public String addActor(#RequestBody Actor actor) {
return actorService.addActor(actor);
}
#GetMapping
public List<Actor> getActors() {
return actorService.getActors();
}
#GetMapping("/{id}")
public List<Actor> getActor(#PathVariable String id) {
return actorService.getActor(id);
}
#DeleteMapping("/{id}")
public String deleteActor(#PathVariable String id) {
return actorService.deleteActor(id);
}
}
ActorService.java:
package com.film.SpringAplication.service;
import java.util.List;
import java.util.Optional;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.film.SpringAplication.model.Actor;
import com.film.SpringAplication.repository.ActorRepository;
import lombok.RequiredArgsConstructor;
#Service
#RequiredArgsConstructor
public class ActorService {
private final ActorRepository actorRepository;
private final MongoTemplate mongoTemplate;
public String addActor(Actor actor) {
actorRepository.save(actor);
return "Actor Added";
}
public List<Actor> getActors() {
List<Actor> lista = actorRepository.findAll();
return lista;
}
public List<Actor> getActor(String id) {
Query query=new Query()
.addCriteria(Criteria.where("actor_id").is(id));
return mongoTemplate.find(query,Actor.class);
//return actorRepository.findById(id);
}
public String deleteActor(String id) {
actorRepository.deleteById(id);
return "User deleted";
}
}
ActorRepository.java:
package com.film.SpringAplication.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.film.SpringAplication.model.Actor;
public interface ActorRepository extends MongoRepository<Actor,String>{
}
What you can do is, you can use #Field. You annotate #Id to actor_id. So basically it takes as the default primary key. You can do two things. You can annotate _id as default primary key.
#Document(collection="actor")
#AllArgsConstructor
#Data
public class Actor {
#Id
ObjectId _id;
String actor_id;
String first_name;
String last_name;
}
Else you can annotate #Field
#Document(collection="actor")
#AllArgsConstructor
#Data
public class Actor {
#Id
#Field("actor_id")
String actor_id;
String first_name;
String last_name;
}
Related answer
I have created a crud application Using spring boot initializer.
Dependencies:
Lombok
Spring Web
Spring Mongo
This app calls from a database/cluster that I have set up on atlas. but I want it to call the correct collection and just do a simple get all api call in postman
but I get a server 500 error
Service Java file:
package com.fullstack.app.Service;
import com.fullstack.app.exception.EntityNotFoundException;
import com.fullstack.app.Model.*;
import com.fullstack.app.Model.Request.WCCreationRequest;
import com.fullstack.app.Repository.StatusData_WCRepo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
#Service
#RequiredArgsConstructor
public class StatusDataService {
private static StatusData_WCRepo wcRepository;
public StatusData createData (WCCreationRequest request) {
StatusData statusData = new StatusData();
BeanUtils.copyProperties(request, statusData);
return wcRepository.save(statusData);
}
public static List<StatusData> getAllData() {
return wcRepository.findAll();
}
}
request:
package com.fullstack.app.Model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Getter;
import lombok.Setter;
#Getter
#Setter
#Document(collection = "StatusData_WC")
public class StatusData {
#Id
private String ID_Number;
private String Surname;
private String Full_Names;
private String Address;
private String VR;
private Integer Ward;
private Integer VD_Number;
}
Controller:
package com.fullstack.app.Controller;
import com.fullstack.app.Model.StatusData;
import com.fullstack.app.Model.Request.WCCreationRequest;
import com.fullstack.app.Service.StatusDataService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import lombok.RequiredArgsConstructor;
#RestController
#RequestMapping(value = "/api/statusData")
#RequiredArgsConstructor
public class StatusDataController {
private final StatusDataService sdService;
#GetMapping("/statusdata")
public ResponseEntity getAllData(#RequestParam(required = false) String id) {
if (id == null) {
return ResponseEntity.ok(StatusDataService.getAllData());
}
return ResponseEntity.ok(StatusDataService.getAllData());
}
}
Application properties:
spring.data.mongodb.uri=mongodb+srv://*****:******#cluster0.wlmmf.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
I'm trying to create delete function with Spring boot, reactjs and axios.
For first step, I just confirm if delete action is activated by entering URL directly.
But It doesn't work even if I enter URL directly.
I know GET is not supported but I don't know which I should fix.
Please tell me if you know.
ActionController.java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.repository.CheckListRepository;
import com.example.demo.service.CheckListService;
#CrossOrigin
#RequestMapping("action/")
#RestController
public class ActionController {
#Autowired
CheckListRepository clr;
#Autowired
CheckListService cls;
#DeleteMapping(path = "{deleteId}")
public void deleteAction(#PathVariable Integer deleteId) {
clr.deletebyListNo(deleteId);
}
}
PS: This is axios code about contents.
Controller
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.CheckList;
import com.example.demo.entity.CheckListForm;
import com.example.demo.repository.CheckListRepository;
import com.example.demo.service.CheckListService;
#CrossOrigin
#RestController
#RequestMapping("api/")
public class CheckListController {
#Autowired
private CheckListRepository checkListRepository;
#Autowired
private CheckListService cls;
#GetMapping("list")
public List<CheckListForm> getList() {
List<CheckList> checkList = this.checkListRepository.findAll();
List<CheckListForm> checkListForm = cls.entityToForm(checkList);
return checkListForm;
}
}
CheckList.js
import axios from 'axios'
const CHECKLIST_REST_API_URL = 'http://localhost:8080/api/list';
class CheckListService {
getList() {
return axios.get(CHECKLIST_REST_API_URL);
}
}
export default new CheckListService();
I could confirm delete action method in control is activated in the following way.
I changed code
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.repository.CheckListRepository;
import com.example.demo.service.CheckListService;
#CrossOrigin
#RequestMapping("action/")
#RestController
public class ActionController {
#Autowired
CheckListRepository clr;
#Autowired
CheckListService cls;
#DeleteMapping(path = "{deleteId}")
public void deleteAction(#PathVariable Integer deleteId) {
clr.deletebyListNo(deleteId);
}
}
to
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.repository.CheckListRepository;
import com.example.demo.service.CheckListService;
#CrossOrigin
#RequestMapping("action/")
#RestController
public class ActionController {
#Autowired
CheckListRepository clr;
#Autowired
CheckListService cls;
#RequestMapping(path = "{deleteId}")
public void deleteAction(#PathVariable Integer deleteId) {
clr.deletebyListNo(deleteId);
}
}
I try to search all data using java-spring-boot with Mysql but when i run the code i got the this error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'emp0_.get_company_name' in 'field list'
This is my Controller code
package com.example.rest.controller;
import com.example.rest.Emp;
import com.example.rest.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
#RequestMapping("emp")
public class EmpController {
#Autowired
private EmpService empService;
#GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<List<Emp>> getAllEmps() {
List<Emp> emps = empService.findAll();
return new ResponseEntity<List<Emp>>(emps,HttpStatus.OK);
}
}
This is my EmpRepository code
package com.example.rest.repository;
import com.example.rest.Emp;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface EmpRepository extends JpaRepository<Emp, Integer> {
}
This is my EmpService Code
package com.example.rest.service;
import com.example.rest.Emp;
import java.util.List;
public interface EmpService {
List<Emp> findAll();
}
This is my EmpServiceImpl Code
package com.example.rest.service;
import com.example.rest.Emp;
import com.example.rest.repository.EmpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
#Service
public class EmpServiceImpl implements EmpService {
#Autowired
private EmpRepository empRepository;
#Override
public List<Emp> findAll() {
List<Emp> emps= new ArrayList<>();
empRepository.findAll().forEach((e -> emps.add(e)));
return emps;
}
}
This is my Application Code
package com.example.rest;
import com.example.rest.repository.EmpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
#SpringBootApplication
#Configuration
public class Application {
#Autowired
EmpRepository empRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class , args);
}
}
This is my Emp Code
package com.example.rest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Table(name = "company")
#Data
#AllArgsConstructor
#NoArgsConstructor
public class Emp implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue (strategy = GenerationType.IDENTITY)
private Integer idx;
private String company_id;
private String getCompany_name;
}
So if someone knows that what is the problem please tell me if you don't remind with solution
Thank you!
Use #Service annotation on the EmpServiceImpl class and #Repository annotation on the EmpRepository.
#Service
public class EmpServiceImpl implements EmpService {
#Autowired
private EmpRepository empRepository;
#Override
public List<Emp> findAll() {
List<Emp> emps= new ArrayList<>();
empRepository.findAll().forEach((e -> emps.add(e)));
return emps;
}
}
#Repository
public interface EmpRepository extends JpaRepository<Emp, Integer> {
}
Need to add annotation on below class:
#Service
class EmpServiceImpl implements EmpService {
}