BACK 

ThirdParty API

ThirdParty API Pre-Requisites: For Application level understanding need Spring, Springboot, RESTful Services, Hibernate Third-Party API APPLICATION PROPERTIES spring.datasource.url=jdbc:mysql://localhost:3306/Thirdparty?allowPublicKeyRetrieval=true&useSSL=false spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update MODEL CLASS Code : package com.example.ThirdPartyy.Model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor @Entity @Table public class TModel { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; @Column private long userId; @Column private String title; @Column private String body; public long getId() { return id; } public void setId(long id) { this.id = id; } public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } } Controller Class Code: package com.example.ThirdPartyy.controller; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; 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.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.ThirdPartyy.Model.TModel; import com.example.ThirdPartyy.Repo.TRepo; import com.example.ThirdPartyy.Service.TService; @RestController @RequestMapping("/thirdparty/api") public class TController { //Controller is dependent on Service layer so service layer is auto-wired; @Autowired private TService tservice; @PostMapping //when posting data into table post request handler is used public ResponseEntity<String> sendPost(@RequestBody TModel tm){ return tservice.sendPost(tm); } @GetMapping("mytable/{postId}") // Get request handler is used to get data by id public ResponseEntity<TModel> getDataa(@PathVariable long postId){ return new ResponseEntity<TModel>(tservice.mytable(postId),HttpStatus.OK); } @GetMapping("{postId}") // public List<TModel> getData(@PathVariable long postId){ return tservice.getData(postId); } @PutMapping("{postId}") public ResponseEntity<String> updateData(@PathVariable long postId,@RequestBody TModel tm){ return tservice.updatePost(postId, tm); } @DeleteMapping("{postId}")//Delete records from table using id public ResponseEntity<Void> deletePost(@PathVariable long postId){ return tservice.deletePost(postId); } @GetMapping("/intotable/{postId}")//this Get request handler is used to get data into table from third party api public ResponseEntity<TModel> getintotable(@PathVariable long postId,@RequestBody TModel tmm){ return new ResponseEntity<TModel>(tservice.getDatatotable(postId, tmm),HttpStatus.OK); } @PutMapping("/mytableupdate/{postId}")//this Put request handler is used to update data in our table public ResponseEntity<TModel> updatemytable(@PathVariable long postId,@RequestBody TModel tmmm){ return new ResponseEntity<TModel>(tservice.updatemytable(postId, tmmm),HttpStatus.CREATED); } } SERVICE INTERFACE CLASS CODE: package com.example.ThirdPartyy.Service; import java.util.List; import org.springframework.http.ResponseEntity; import com.example.ThirdPartyy.Model.TModel; public interface TService { //postmapping ResponseEntity<String> sendPost(TModel tm); List<TModel> getData(long postId); TModel getDatatotable(long postId,TModel tm); TModel mytable(long postId); ResponseEntity<String> updatePost(long postId,TModel tm); ResponseEntity<Void> deletePost(long postId); TModel updatemytable(long postId,TModel tmmm); } SERVICE IMPLEMENTATION CLASS CODE: package com.example.ThirdPartyy.ServiceImp; import java.util.List; import java.util.Optional; import java.util.Arrays; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.example.ThirdPartyy.Model.TModel; import com.example.ThirdPartyy.Repo.TRepo; import com.example.ThirdPartyy.Service.TService; @Service public class TPService implements TService { @Autowired private TRepo repo; RestTemplate resttemp=new RestTemplate(); HttpHeaders headers=new HttpHeaders(); String url="https://jsonplaceholder.typicode.com/posts/"; //sendpost @Override public ResponseEntity<String> sendPost(TModel tm) { headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<TModel> requestEntity= new HttpEntity<>(tm,headers); repo.save(tm); ResponseEntity<String> re=resttemp.postForEntity(url, requestEntity, String.class); return ResponseEntity.status(re.getStatusCode()).body(re.getBody()); } //getData public List<TModel> getData(long postId){ String url1=url+postId; TModel tm=resttemp.getForObject(url1, TModel.class); repo.findById(postId); return Arrays.asList(tm); } //updatePost @Override public ResponseEntity<String> updatePost(long postId, TModel tm) { String url2=url+postId; headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<TModel> requestEntityy=new HttpEntity<>(tm,headers); ResponseEntity<String> ree=resttemp.postForEntity(url2, requestEntityy, String.class); repo.save(tm); return ResponseEntity.status(ree.getStatusCode()).body(ree.getBody()); } //deletePost public ResponseEntity<Void> deletePost(long postId){ String url3=url+postId; headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<?> requestEntityyy=new HttpEntity<>(headers); repo.deleteById(postId); resttemp.delete(url3,requestEntityyy); return ResponseEntity.noContent().build(); } //getbyidfrommytable @Override public TModel mytable(long postId) { Optional<TModel> op=repo.findById(postId); if(op.isPresent()) { return op.get(); } return null; } //getDataTotable @Override public TModel getDatatotable(long postId,TModel tmm) { String url5=url+postId; TModel tm=resttemp.getForObject(url5, TModel.class); return repo.save(tm); } //updatemytable @Override public TModel updatemytable(long postId, TModel tmmm) { Optional<TModel> op=repo.findById(postId); if(op.isPresent()) { op.get().setUserId(tmmm.getId()); op.get().setTitle(tmmm.getTitle()); op.get().setBody(tmmm.getBody()); return repo.save(op.get()); } return null; } } REPOSITORY CLASS CODE: package com.example.ThirdPartyy.Repo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.ThirdPartyy.Model.TModel; @Repository public interface TRepo extends JpaRepository<TModel,Long>{ } BACK