Search / object register via REST url and register with SPRING

3

I'm new to REST and Spring. I'm doing an exercise where I have to fetch a client via url and return (GET) it in JSON format and insert (POST) a client in JSON format.

To search will be in the template below, using GET:

http://localhost:8080/clientes/?nome=paulo

and return:

{
     "nome": "paulo",
     "cpf": 1231243434,
     "idade": 34,
     "sexo": M,
}

to insert into the template using POST:

{
     "nome": "paulo",
     "cpf": 1231243434,
     "idade": 34,
     "sexo": M,
}

This uses only the memory of instantiated objects.

My classes:

Customer

package model;
public class Cliente {

private String nome;
private String cpf;
private Integer idade;
private String sexo;

public Cliente(String nome, String cpf, Integer idade, String sexo) {
    this.nome = nome;
    this.cpf = cpf;
    this.idade = idade;
    this.sexo = sexo;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCpf() {
    return cpf;
}

public void setCpf(String cpf) {
    this.cpf = cpf;
}

public Integer getIdade() {
    return idade;
}

public void setIdade(Integer idade) {
    this.idade = idade;
}

public String getSexo() {
    return sexo;
}

public void setSexo(String sexo) {
    this.sexo = sexo;
}

@Override
public String toString() {
    return "Cliente [nome=" + nome + ", cpf=" + cpf + ", idade=" +   idade + ", sexo=" + sexo + "]";
}
}

ClientController

package controller;

import java.util.Hashtable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import model.Cliente;
import service.ClienteService;

@RestController
@RequestMapping("/clientes")
public class ClienteController {


@Autowired
ClienteService cli;

@RequestMapping("/all")
public Hashtable<String, Cliente> getAll(){
    return cli.getAll();
}

//não esta buscando desta forma
@RequestMapping("{nome}")
public Cliente getCliente(@PathVariable("nome") String nome){   
    return cli.getCliente(nome);
}
}

ClientService

package service;
import java.util.Hashtable;
import org.springframework.stereotype.Service;
import model.Cliente;

@Service
public class ClienteService {
Hashtable<String, Cliente> clientes = new Hashtable<String, Cliente>      ();

public ClienteService(){

    Cliente c1 = new Cliente("paulo","123.123.123-22",28,"M");
    Cliente c2 = new Cliente("diego","123.123.123-22",24,"M");
    Cliente c3 = new Cliente("Debora","123.123.123-22",21,"F");

    clientes.put("1", c1);
    clientes.put("2", c2);
    clientes.put("3", c3);

}

public Cliente getCliente(String nome){
    if(clientes.containsKey(nome)){
        return clientes.get(nome);
    }else{
        return null;
    }

}
public Hashtable<String, Cliente> getAll(){
    return clientes;
}

}

So far I've been able to show all clients instantiated in JSON format, I've done some research and I'm not able to solve this.

    
asked by anonymous 28.10.2016 / 00:42

3 answers

2

You are trying to query query param ( clientes?nome=paulo ) but implemented a search for param param . (% with%)

Try changing your search to:

@RequestMapping(method = RequestMethod.GET)
public Cliente getCliente(@RequestParam("nome") String nome){   
    return cli.getCliente(nome);
}

And, taking advantage of that, correct your other query by removing clientes/paulo at the end. In Rest good practices, when you call a feature without mentioning anything, you will be asking for all available features. To do this, change this way:

@RequestMapping(method = RequestMethod.GET)
public Hashtable<String, Cliente> getAll(){
    return cli.getAll();
}

And just do it:

http://localhost:8080/clientes/

To return all clients

    
28.10.2016 / 00:53
1

Take a look at the documentation but to get a parameter it uses the @RequestParam annotation.

Documentation: link

@RequestMapping(method = RequestMethod.GET) public String setupForm(@RequestParam("petId") int petId, ModelMap model) { Pet pet = this.clinic.loadPet(petId); model.addAttribute("pet", pet); return "petForm"; }

    
28.10.2016 / 00:52
0

Hello, youngster!

You are using "@PathVariable" instead of "@RequestParam". If so, the code should look like this:

@RequestMapping(value = "/{nome}", method = RequestMethod.GET)
public Cliente getCliente(@PathVariable("nome") String nome){   
    return cli.getCliente(nome);
}

Then you will have to give the GET through the URI:

http://localhost:8080/clientes/paulo

There are other ways to do this. If your intent is to just search by name yourself, you can attack like this:

@RequestMapping(method = RequestMethod.GET)
public Cliente getCliente(@RequestParam("nome") String nome){   
    return cli.getCliente(nome);
}

Or you can still attack like this:

@RequestMapping(method = RequestMethod.GET)
public Cliente getCliente(Cliente cliente){   
    return cli.getCliente(cliente.getNome());
}

For these two examples, the uri is the one you wrote:

http://localhost:8080/clientes?nome=paulo

In the second case, spring itself takes care of transforming what you send to the client. Since you will want to transition Client, you need to implement Serializable.

public class Cliente implements Serializable{
    private static final long serialVersionUID = 1L;

Any questions, just ask.

    
28.10.2016 / 14:56