【发布时间】:2018-02-17 19:51:59
【问题描述】:
我创建了一个 spring data jpa 应用程序。在这个应用程序中,我的方法请求是 GET。但如果我试图访问该方法请求 url 作为发布请求。在这种情况下,我想知道如何在我的自定义错误消息中添加 HTTP 状态代码 405(Method Not Allowed)。
这是我的代码 sn-p
部门模型
package com.demo.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "department")
public class DepartmentModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
public Integer ndeptid;
public String sdeptname ;
public Integer ninstid ;
public Boolean bislocked;
public String sclientdeptid;
public Integer nsurveymethodid;
public Boolean bisjointuse;
public Integer ntempdeptid;
public Boolean balternatejointusepercentage;
public Integer ndivid;
//getter and setter
部门存储库
@Repository
public interface DepaertmentRepository extends JpaRepository<DepartmentModel, Integer>
{
@Query("select new map(dep.sdeptname as sdeptname)"
+ " from DepartmentModel as dep where dep.ninstid=60")
Set<DepartmentModel> findBySDepName();
}
部门服务
@Service
public class DepartmentService
{
@Autowired
DepaertmentRepository depRepo;
public Set<DepartmentModel> findDepName()
{
return depRepo.findBySDepName();
}
}
部门主管
@RestController
@RequestMapping("/SpaceStudy/SpaceAdmin")
public class DepartmentController {
@Autowired
DepartmentService depService;
@CrossOrigin(origins="*")
@GetMapping("AccountMaintenance/LoadDepartment")
//@ResponseStatus( value = HttpStatus.METHOD_NOT_ALLOWED)
public Set<DepartmentModel> findDepName() {
return depService.findDepName();
}
}
当我以帖子形式访问 GET 请求时,任何人都可以帮助我如何添加带有正确消息的 HTTP 状态代码 (405)
【问题讨论】:
标签: spring-data-jpa http-status-code-405