0

Sorry I'm starting on the site. How can I make a controller Generic restfull post JSON? My DAOS and services work perfectly. DAOS already are ready more do not know where to start ..!

I tried to do so:

public class AbstractControllerCrud<T extends IDomainObject> {

    protected IGenericService<T> service;

    public AbstractControllerCrud(IGenericService<T> service) {
        this.service = service;
    }

    @RequestMapping(value = "all", method = RequestMethod.GET)
    protected
    @ResponseBody
    List<T> getAll() {
        List<T> listaGenerica = new ArrayList<>();
        listaGenerica = service.getBeans();
        return listaGenerica;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    protected
    @ResponseBody
    T get(@PathVariable int id) {
        Long idRequest = (long) id;
        T retorno = this.service.getObjeto(idRequest);
        return retorno;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseBody
    protected void delete(@PathVariable int id) {
        Long idRequest = (long) id;
        T T = this.service.getObjeto(idRequest);
        service.deleta(T);
    }
}

but it does not work: error 404;

@Controller("/test")
public class TestController extends AbstractControllerCrud<Profissoes> {
    @Autowired
    public TestController(@Qualifier("profissoesServicesImpl") IGenericService<Profissoes> profissaoService) {
        super(profissaoService);
    }
}

this works, it does not display 404.

@Controller
@RequestMapping("/profissoes")
public class ProfissoesController {

    @Autowired
    private IProfissoesService profissaoServ;

    @RequestMapping(value = "all", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Profissoes> getProfissoes() {
        List<Profissoes> listaProfissoes = new ArrayList<>();
        listaProfissoes = profissaoServ.getBeans();
        return listaProfissoes;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public
    @ResponseBody
    Profissoes get(@PathVariable int id) {
        Long idRequest = (long) id;
        Profissoes profissoes = this.profissaoServ.getObjeto(idRequest);
        return profissoes;
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable int id) {
        Long idRequest = (long) id;
        Profissoes profissao = this.profissaoServ.getObjeto(idRequest);
        profissaoServ.deleta(profissao);
    }
}
1
  • Hi guys I Need Generic Controller.. Commented Dec 17, 2014 at 20:21

2 Answers 2

1

angulrjs controller:

yourAppName.controller('myController' , [$http , $scope , function($http , $scope)
{
 var paramValue = [];
 paramValue['par'] = $scope.someValue;
$http.get('rest/getdata' , {params : paramValue}).success(function(data)
{
   $scope.myData = data;

}).error(function(data)
{
   console.log('error occured');

});

}]);

spring controller:

@RequestMapping(value ="/getdata",method = RequestMethod.GET , params = "par")  
public @ResponseBody  
List<Profissoes> getProfissoes(@RequestMapping("par") long value)  {
        System.out.println("Value "+value);
        // use it further the way u want
    } 
Sign up to request clarification or add additional context in comments.

Comments

0

So as I think u want make requests from angular like: .../all?itemType=Project .../all?itemType=User and so on... Why not then:

public @ResponseBody List<? extends IItem> getAll(@RequestParam String itemType) {
    IService<? extends IItem> service = ServiceFactory.get(itemType);
    // or
    IService<? extends IItem> service = context.getBean(itemType);
    return service.getAll();
}

Anyway, unlikely that spring can guess object type from json...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.