Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/16.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 Spring Data REST JPA:将自动积垢操作与手动控制器集成_Java_Json_Spring_Jpa - Fatal编程技术网

Java Spring Data REST JPA:将自动积垢操作与手动控制器集成

Java Spring Data REST JPA:将自动积垢操作与手动控制器集成,java,json,spring,jpa,Java,Json,Spring,Jpa,我正在使用SpringDataRESTJPA构建一个RESTfulWeb服务。到目前为止,Spring正在自动生成所有可能方法的所有响应,列出所有可用资源,甚至搜索它们: @RepositoryRestResource(collectionResourceRel = "scans", path = "scans") public interface ScanRepository extends PagingAndSortingRepository<Scan, Long> { Lis

我正在使用SpringDataRESTJPA构建一个RESTfulWeb服务。到目前为止,Spring正在自动生成所有可能方法的所有响应,列出所有可用资源,甚至搜索它们:

@RepositoryRestResource(collectionResourceRel = "scans", path = "scans")
public interface ScanRepository extends PagingAndSortingRepository<Scan, Long> {

List<Scan> findByProjectId(@Param("pid") String pid);
}
然而,当我这样做时,JPA数据自动生成的所有其他方法和搜索等的响应就不再存在了。例如,如果我转发一个get请求,我会得到“methodnotallowed”

此外,我如何从控制器访问JSON负载

更新

现在,对于我自己的控制器中没有手动处理的请求,只有一个公开的资源返回到默认方法。但是,我不知道为什么会发生这种情况,以及为什么其他任何资源都不会发生这种情况。* 尽管如此,它们都只是在实体属性上有所不同

以下特定资源将返回到默认请求处理程序,以处理我在控制器中声明的不是POST scan/或GET/scan///的任何内容:

@Controller
public class ScanController {

    @Autowired
    private ScanService scanService;

    @RequestMapping(
            value = "/scan",
            method = POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody
    Scan parseScan(@RequestBody Scan rbody) {
                    <...do something...>
    }

    @RequestMapping(value = "/scans/{id}/{totvuln}/{nth}", method = RequestMethod.GET,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody
    Scan getScan(@PathVariable String id, @PathVariable int totvuln, @PathVariable int nth) throws ScanNotFound {
        <...do something...>
}
@RequestMapping(
            value = "/scan",
            method = POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody
    Scan parseScan(@RequestBody Scan rbody) {
        Scan scan = new Scan();
        scan.setProjectId(rbody.getProjectId());
        scan.setTool(rbody.getTool());
        return scan;
    }
@控制器
公共类扫描控制器{
@自动连线
私人扫描服务;
@请求映射(
value=“/scan”,
方法=员额,
consumes=MediaType.APPLICATION\u JSON\u值,
products={MediaType.APPLICATION_JSON_VALUE})
公共@ResponseBody
扫描解析扫描(@RequestBody Scan-rbody){
}
@RequestMapping(value=“/scans/{id}/{totvuln}/{nth}”,method=RequestMethod.GET,
products={MediaType.APPLICATION_JSON_VALUE})
公共@ResponseBody
Scan getScan(@PathVariable String id、@PathVariable int totvuln、@PathVariable int nth)抛出ScanNotFound{
}
它具有以下存储库界面:

public interface ScanRepository extends PagingAndSortingRepository<Scan, Long> {}
public interface RulesRepository extends PagingAndSortingRepository<Rules, Long> {}
公共接口扫描存储库扩展了分页和排序存储库{}
及以下服务:

@Service
public class ScanServiceImpl implements ScanService {

    @Resource
    private ScanRepository scanRepository;

    @Resource
    private ResultRepository resultRepository;

    @Override
    @Transactional
    public Scan create(Scan shop) {
        <some code>
    }

    @Override
    @Transactional
    public Scan findById(long id) {
        <some code>
    }

    @Override
    @Transactional(rollbackFor = ScanNotFound.class)
    public Scan delete(long id) throws ScanNotFound {
                    <some code>
    }

    @Override
    @Transactional
    public List<Scan> findAll() {
                    <some code>
    }

    @Override
    @Transactional(rollbackFor = ScanNotFound.class)
    public Scan update(Scan scan) throws ScanNotFound {
                    <some code>
    }
}
@服务
公共类ScanServiceImpl实现ScanService{
@资源
私人扫描库扫描库;
@资源
私人结果证明;
@凌驾
@交易的
公共扫描创建(扫描商店){
}
@凌驾
@交易的
公共扫描findById(长id){
}
@凌驾
@事务性(rollboor=ScanNotFound.class)
公共扫描删除(长id)抛出ScanNotFound{
}
@凌驾
@交易的
公共列表findAll(){
}
@凌驾
@事务性(rollboor=ScanNotFound.class)
公共扫描更新(扫描扫描)抛出ScanNotFound{
}
}
并且资源本身具有以下属性:

@Entity
public class Scan {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long projectId;

    @OneToMany
    private Collection<Result> result;

    private int totV;

    <getters and setters>
}
@Entity
public class Rules {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne
    private Scan scan;

    @OneToMany
    private Collection<Result> result;

    private String rules;

    <getters and setters>
}
@实体
公共类扫描{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私人长投射;
@独身癖
私人收藏结果;
私人int totV;
}
而以下半相同的资源“规则”不会返回到任何默认请求处理程序。对于与POST/rule不同的任何内容,它都会返回“不允许使用方法”:

@Controller
public class RulesController {

    @Autowired
    private RulesService rService;

    @Resource
    private ScanRepository scanRepository;

    @RequestMapping(
            value = "/rule",
            method = POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody
    Rules generateRules(@RequestBody Scan rbody) throws Exception {
       <do something>
    }
}
@控制器
公共类规则控制器{
@自动连线
私人规则服务;
@资源
私人扫描库扫描库;
@请求映射(
value=“/rule”,
方法=员额,
consumes=MediaType.APPLICATION\u JSON\u值,
products={MediaType.APPLICATION_JSON_VALUE})
公共@ResponseBody
规则生成器(@RequestBody Scan rbody)引发异常{
}
}
它具有相同的存储库界面:

public interface ScanRepository extends PagingAndSortingRepository<Scan, Long> {}
public interface RulesRepository extends PagingAndSortingRepository<Rules, Long> {}
公共接口规则存储库扩展了分页和排序存储库{}
以及相同的服务实现:

@Service
public class RulesServiceImpl implements RulesService {

    @Resource
    private RulesRepository rRepository;

    @Resource
    private ResultRepository resultRepository;

    @Override
    @Transactional
    public Rules create(Rules shop) {        
        <do something>
    }

    @Override
    @Transactional
    public Rules findById(long id) {
        <do something>
    }

    @Override
    @Transactional(rollbackFor = RulesNotFound.class)
    public Rules delete(long id) throws RulesNotFound {
                <do something>
    }

    @Override
    @Transactional
    public List<Rules> findAll() {
        <do something>
    }

    @Override
    @Transactional
    public Rules findByScanId(long id) throws RulesNotFound {
        <do something>
    }

    @Override
    @Transactional(rollbackFor = RulesNotFound.class)
    public Rules update(Rules scan) throws RulesNotFound {
        <do something>
    }
}
@服务
公共类RulesServiceImpl实现RulesService{
@资源
私人规则的推定;
@资源
私人结果证明;
@凌驾
@交易的
公共规则创建(规则商店){
}
@凌驾
@交易的
公共规则findById(长id){
}
@凌驾
@事务性(rollboor=RulesNotFound.class)
公共规则删除(长id)抛出规则查找{
}
@凌驾
@交易的
公共列表findAll(){
}
@凌驾
@交易的
公共规则findByScanId(长id)抛出规则查找{
}
@凌驾
@事务性(rollboor=RulesNotFound.class)
公共规则更新(规则扫描)抛出规则查找{
}
}
资源规则本身具有以下属性:

@Entity
public class Scan {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long projectId;

    @OneToMany
    private Collection<Result> result;

    private int totV;

    <getters and setters>
}
@Entity
public class Rules {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne
    private Scan scan;

    @OneToMany
    private Collection<Result> result;

    private String rules;

    <getters and setters>
}
@实体
公共阶级规则{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
@奥内托内
私人扫描;
@独身癖
私人收藏结果;
私有字符串规则;
}
为什么Spring不为我的控制器类中未手动指定的任何请求公开默认请求处理程序的“规则”


如果您能指出原因,我将不胜感激。非常感谢!

我已经知道如何从控制器访问JSON负载:

@Controller
public class ScanController {

    @Autowired
    private ScanService scanService;

    @RequestMapping(
            value = "/scan",
            method = POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody
    Scan parseScan(@RequestBody Scan rbody) {
                    <...do something...>
    }

    @RequestMapping(value = "/scans/{id}/{totvuln}/{nth}", method = RequestMethod.GET,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody
    Scan getScan(@PathVariable String id, @PathVariable int totvuln, @PathVariable int nth) throws ScanNotFound {
        <...do something...>
}
@RequestMapping(
            value = "/scan",
            method = POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public @ResponseBody
    Scan parseScan(@RequestBody Scan rbody) {
        Scan scan = new Scan();
        scan.setProjectId(rbody.getProjectId());
        scan.setTool(rbody.getTool());
        return scan;
    }
此外,我还意识到,自动CRUD操作实际上已经得到了我自己的控制器未处理的每个请求的支持:我只是请求了错误的URL。 我通过请求“curl”得到了要使用的正确URL列表

但是,任何自动生成的操作的首选URL都可以使用

@RepositoryRestResource(collectionResourceRel = pref_URL_suffix, path = pref_URL_suffix)
^^不知何故,在我尝试的所有更改中,上面的那一行丢失了