Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实现20@RestController';在spring框架中,无需重复您自己(干燥)_Java_Spring_Spring Data - Fatal编程技术网

Java 实现20@RestController';在spring框架中,无需重复您自己(干燥)

Java 实现20@RestController';在spring框架中,无需重复您自己(干燥),java,spring,spring-data,Java,Spring,Spring Data,在实现了大约20个rest控制器和服务之后,我发现我的很多代码都被重复了,所以我想出了这个装置 CrudController.java 软件包app.controllers import org.springframework.data.repository.CrudRepository; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; i

在实现了大约20个rest控制器和服务之后,我发现我的很多代码都被重复了,所以我想出了这个装置

CrudController.java 软件包app.controllers

import org.springframework.data.repository.CrudRepository;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import app.models.Model;
import app.services.CrudService;

@RestController
public abstract class CrudController<M extends Model, S extends CrudService<M, ? extends CrudRepository<M,Long>>> {
    S service;

    public abstract void setService(S service);
    public abstract Boolean isAuthorized(Long entityId, S service);

    @RequestMapping(value="/create", method = RequestMethod.POST)
    public M create(M object) {
        if(isAuthorized(object.getId(), service)) {
            return service.save(object);
        }
        logUnauthorizedAccess();
        return null;
    }

    @RequestMapping(value="/update", method = RequestMethod.POST)
    public M update(M object) {
        if(isAuthorized(object.getId(), service)) {
            return service.update(object);
        }
        logUnauthorizedAccess();
        return null;
    }

    @RequestMapping(value="/delete", method = RequestMethod.POST)
    public Boolean delete(Long id) {
        if(isAuthorized(id, service)) {
            return service.delete(id);
        }
        logUnauthorizedAccess();
        return null;
    }

    @RequestMapping(value="/get", method = RequestMethod.GET)
    public @ResponseBody M get(Long id) {
        if(isAuthorized(id, service)) {
            return service.get(id);
        }
        logUnauthorizedAccess();
        return null;
    }


    @RequestMapping(value="/json", method = RequestMethod.GET)
    public @ResponseBody Iterable<M> json(ModelMap map) {
        return service.getAll();
    }

    private void logUnauthorizedAccess() {
        System.out.println("!!UN-AUTHORIZED ACCESS DETECTED!!");
    }
}
现在我所有的控制器在给我。 /创建/get/update/delete/json

package app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import app.models.Sample;
import app.services.SampleService;

@RestController
@RequestMapping("/sample")
public class SampleController extends CrudController<Sample, SampleService> {

    @Autowired
    @Override
    public void setService(SampleService service) {
        this.service = service;
    }

    @Override
    public Boolean isAuthorized(Long entityId, SampleService service) {
        return true;
    }
}
package app.controllers;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入app.models.Sample;
导入app.services.SampleService;
@RestController
@请求映射(“/sample”)
公共类SampleController扩展了CrudController{
@自动连线
@凌驾
公共无效设置服务(SampleService服务){
服务=服务;
}
@凌驾
公共布尔值已授权(长entityId,SampleService服务){
返回true;
}
}


你们认为,好的坏的,更好的方法是什么?

Spring能够处理泛型:

只要内部实现不变,就不需要为每个
模型
CrudController
进行子类化

例如,您可以使用一个使用类的
PathVariable
的控制器

然后您也可以使用
CrudService
Dao

如果您现在看到,对于一些需要在此控制器(或服务,dao)中进行特殊处理的实体,我知道至少有两种方法:

  • 使用控制器、服务和dao为此实体类型创建一个完整的新路由
  • 不要简单地在crudcontroller中使用crudservice,而是自动连接列表(或地图)并查找正确的服务以继续路由
第二层或第三层中的这种分叉主要取决于您需要有特殊可能性的地方

我已经看到了这两种方法,目前我在spring项目中混合使用了这两种方法。
由于dispatcher servlet根据您拥有的变量数量对requestmappings进行排序,因此很容易创建更具体的映射,只需为专用控制器使用pathvariable对该部分进行硬编码即可。

Spring能够处理泛型:

只要内部实现不变,就不需要为每个
模型
CrudController
进行子类化

例如,您可以使用一个使用类的
PathVariable
的控制器

然后您也可以使用
CrudService
Dao

如果您现在看到,对于一些需要在此控制器(或服务,dao)中进行特殊处理的实体,我知道至少有两种方法:

  • 使用控制器、服务和dao为此实体类型创建一个完整的新路由
  • 不要简单地在crudcontroller中使用crudservice,而是自动连接列表(或地图)并查找正确的服务以继续路由
第二层或第三层中的这种分叉主要取决于您需要有特殊可能性的地方

我已经看到了这两种方法,目前我在spring项目中混合使用了这两种方法。
由于dispatcher servlet根据您拥有的变量数量对requestmappings进行排序,因此很容易创建一个更具体的映射,只需使用pathvariable为专用控制器对该部分进行硬编码即可。

您应该查看一下。

您应该查看一下。

这样,您的端点就不是很安静;url中有动词,而您可以只使用http动词;url中有动词,而您可以只使用http动词。
package app.models;

import java.sql.Timestamp;
import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.Version;

@MappedSuperclass
public abstract class Model {
    @GeneratedValue
    @Id
    private Long id;

    private Date dateCreated;

    @Version
    private Timestamp dateModified;

    @PrePersist
    void createdAt() {
        this.setDateCreated(new Date());
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Timestamp getDateModified() {
        return dateModified;
    }

    public void setDateModified(Timestamp dateModified) {
        this.dateModified = dateModified;
    }
}
package app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import app.models.Sample;
import app.services.SampleService;

@RestController
@RequestMapping("/sample")
public class SampleController extends CrudController<Sample, SampleService> {

    @Autowired
    @Override
    public void setService(SampleService service) {
        this.service = service;
    }

    @Override
    public Boolean isAuthorized(Long entityId, SampleService service) {
        return true;
    }
}