【问题标题】:how make abstract restfull basic with Spring mvc Controller for angular js?如何使用 Spring mvc Controller 为 Angular js 制作抽象的 restfull 基础?
【发布时间】:2014-12-17 18:09:02
【问题描述】:

抱歉,我从网站开始。 如何制作控制器通用 restfull 后 JSON? 我的 DAOS 和服务完美运行。 DAOS 已经准备好了更多不知道从哪里开始..!

我试过这样做:

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);
    }
}

但它不起作用:错误404;

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

这有效,它不显示 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);
    }
}

【问题讨论】:

  • 大家好,我需要通用控制器..

标签: java angularjs spring-mvc


【解决方案1】:

angulrjs 控制器:

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');

});

}]);

弹簧控制器:

@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
    } 

【讨论】:

    【解决方案2】:

    所以我认为你想从角度提出请求,例如: .../all?itemType=项目 .../all?itemType=用户 等等... 那为什么不呢:

    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();
    }
    

    无论如何,spring 不可能从 json 中猜出对象类型...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      相关资源
      最近更新 更多