我正在使用Spring Boot和MongoDB构建一个Web应用程序,它只是为员工文档执行CRUD操作。
当我尝试使用json命中create employee端点时,我收到此错误$scope.active={item:null}
。
我的控制器类是:
"Request method 'POST' not supported"
申请类:
@RestController
@RequestMapping("/employeeapp/employees")
public class EmployeeController {
private final EmployeeService service;
@Autowired
public EmployeeController(EmployeeService service) {
this.service = service;
}
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public
@ResponseBody
Employee create(@RequestBody @Valid Employee employeeEntry) {
return service.create(employeeEntry);
}
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public Employee delete(@PathVariable("id") long id) {
return service.delete(id);
}
@RequestMapping(method = RequestMethod.GET)
public List<Employee> findAll() {
return service.findAll();
}
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public Employee findById(@PathVariable("id") long id) {
return service.findById(id);
}
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
public Employee update(@RequestBody @Valid Employee employeeEntry) {
return service.update(employeeEntry);
}
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleEmployeeNotFound(EmployeeNotFoundException exception) {
}
}
我尝试停用@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
,在方法上添加csrf
但似乎没有任何效果。
修改
我正在使用POST请求点击@ResponseBody
。标题包括http://localhost:8080/employeeapp/employees
和正文中的json:
Content-Type : application/json
此外,这是我在使用POST请求点击上述URL时在日志中看到的内容。
{
"id" : 1,
"name" : "nikhil",
"dept" : "DCX"
}
编辑2:
我检查了Spring启动日志,结果是没有生成映射,而是spring映射到默认服务。知道为什么会这样吗?
2016-02-19 12:21:36.549 INFO 5148 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] :
Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-02-19 12:21:36.549 INFO 5148 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet :
FrameworkServlet 'dispatcherServlet': initialization started
2016-02-19 12:21:36.562 INFO 5148 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet :
FrameworkServlet 'dispatcherServlet': initialization completed in 13 ms
2016-02-19 12:21:36.595 WARN 5148 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound :
Request method 'POST' not supported
答案 0 :(得分:4)
以下可能有所帮助。
以下是可能发生的不同事情。不需要遵循顺序。
编辑:
运行以下命令检查您的控制器是否正在由弹簧应用程序启用和考虑。
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
答案 1 :(得分:3)
如果您的@SpringBootApplication带注释的类和控制器位于不同的包中,则默认类扫描不会选择它。
它几乎适用于所有演示,因为bootstrapping类(@SpringBootApplication带注释的类)和rest控制器类驻留在同一个包中并加载到spring上下文。
//让您的班级可以像春天一样进入春天
@SpringBootApplication(scanBasePackageClasses = {GreetingController.class})
public class BootPocApplication {
OR //像这样给出基本包名称
@SpringBootApplication(scanBasePackages = {"com.tryout"})
public class BootPocApplication {
答案 2 :(得分:0)
对我来说,当我在方法定义中使用 @RequestParam 时发生了这个错误:
@RequestMapping(value = "/balance",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public FinancialMsgResponse balance(@RequestParam(value = "requestMessage") FinancialMsgRequest requestMessage) throws Exception {
return new FinancialMsgResponse();
}
所以当我删除@RequestParam
错误
请求方法&#39; POST&#39;不支持
消失了。
祝你好运答案 3 :(得分:0)
只需遵循
@Controller
@RequestMapping(
method={RequestMethod.POST,RequestMethod.GET,RequestMethod....}
)
public class YourController{
.
.
.
}
答案 4 :(得分:0)
对我来说,问题在于@RequestBody类由于是内部类而无法访问。
将其设为公开/静态可访问并解决了该问题
public static class MyClassDto {
public String name;
public String comment;
public String key;
}