aula11tRESTApi
Aula11tRestApiApplication.java
package pd.aula11tRESTApi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Aula11tRestApiApplication { public static void main(String[] args) { SpringApplication.run(Aula11tRestApiApplication.class, args); } }
Country.java
package pd.aula11tRESTApi; public class Country { private String nome; private int population; public Country(String nome, int population) { this.nome = nome; this.population = population; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public int getPopulation() { return population; } public void setPopulation(int population) { this.population = population; } }
HelloWorld.java
package pd.aula11tRESTApi; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("hello-world") public class HelloWorld { //http://localhost:8080/hello-world?name=pedro //curl -i http://localhost:8080/hello-world?name=pedro /* //v1 @GetMapping("") public String get1(@RequestParam(value="name", required = false)String req_name){ if(req_name != null){ return "Hello " + req_name + "!"; }else{ return "Hello world"; } } @GetMapping("{name}") public String get2(@PathVariable("name") String provided_name){ return "Hello " + provided_name + "!"; } */ /* //v2 @GetMapping(value = {"","{name}"}) public String get1(@RequestParam(value="name", required = false)String req_name, @PathVariable(value = "name", required = false) String path_name){ if(path_name != null){ return "Hello " + path_name + "!"; }else if(req_name != null){ return "Hello " + req_name + "!"; }else{ return "Hello world"; } } */ }
HelloWorldDB.java
package pd.aula11tRESTApi; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; public class HelloWorldDB { @GetMapping("world/countries") public List<String> getCountries(@RequestParam(value="name", required=false) String name, @RequestParam(value="min-pop", required=false) String min_population) throws SQLException { List<String> countries = new ArrayList<>(); try( Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/world", "root", ""); Statement st = conn.createStatement()){ String query = "select name, population from country"; if(name!=null){ query += " where name like '%" + name +"%'"; if (min_population!=null){ query += " and population>=" + min_population; } }else if(min_population!=null){ query += " where population>=" + min_population; } ResultSet rs = st.executeQuery(query); while(rs.next()){ countries.add(rs.getString("name")); } }catch(SQLException ex){ System.out.println(ex); throw ex; } return countries; } @GetMapping("world/countries/{name}") public ResponseEntity getCountries(@PathVariable("name") String name){ Country result; try( Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/world", "root", ""); Statement st = conn.createStatement()){ String query = "select name, population from country where name='"+name+"'"; ResultSet rs = st.executeQuery(query); if(rs.next()){ result = new Country(rs.getString("name"), rs.getInt("population")); }else{ return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Country " + name + " not found!"); } }catch(SQLException ex){ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.toString()); } return ResponseEntity.status(HttpStatus.OK).body(result); } }
Tags : PD2122, programação distribuída
0 thoughts on “aula11tRESTApi”