调用下面的第一个Request map(// 1)函数并返回响应。 (暂时没有,数据库中没有数据。
第二个映射如下(path = testnow)与不同的映射完全相同,但返回404?为什么?
package io.egen.rest.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.egen.rest.entity.Movie;
import io.egen.rest.service.MovieService;
@RestController
@RequestMapping(path = "movies")
public class MovieController {
@Autowired
MovieService service;
//1
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<Movie> findAll() {
return service.findAll();
}
@RequestMapping(method = RequestMethod.GET, path = "testnow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<Movie> testUrlNow() {
return service.findAll();
}
@RequestMapping(method = RequestMethod.GET, path = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Movie findOne(@PathVariable("id") String movieId) {
return service.findOne(movieId);
}
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Movie create(@RequestBody Movie movie) {
return service.create(movie);
}
@RequestMapping(method = RequestMethod.PUT, path = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Movie update(@PathVariable("id") String empId, @RequestBody Movie emp) {
return service.update(empId, emp);
}
@RequestMapping(method = RequestMethod.DELETE, path = "{id}")
public void delete(@PathVariable("id") String empId) {
service.delete(empId);
}
}
答案 0 :(得分:0)
如果您正在访问此/movies/testnow
之类的testnow端点,那么路径应更改如下。
@RequestMapping(method = RequestMethod.GET, path = "/testnow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<Movie> testUrlNow() {
return service.findAll();
}
如果不是,则必须将testnow端点称为/moviestestnow
答案 1 :(得分:0)
我不确定它是否有所作为,但我查看了我的旧项目的一些例子,它已经
@RequestMapping(value = "/application")
public class ApplicationController {
...
@RequestMapping(value = "/{id}", method = RequestMethod.GET) {
....
}
}
注意注释参数是&#34;值&#34;而不是&#34;路径&#34; ..
答案 2 :(得分:0)
OKAY!你不会相信这个!
重启Eclipse解决了这个问题!我现在可以打两个网址了!
(可能是缓存/项目清理/服务器清理/缓存实体映射等问题)
这可能是一种陈词滥调,但有时候“把它转过来再把它重新打开”它绝对有效!
感谢大家的帮助,真的很感激。
我知道这是一个愚蠢的帖子。但也许像我这样的人会从中受益!所以我要让这个问题熬夜/。
答案 3 :(得分:-1)
@Autowired
MovieService service;
//1
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public List<Movie> findAll() {
return service.findAll();
}
@RequestMapping(method = RequestMethod.GET, path = "/testnow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public List<Movie> testUrlNow() {
return service.findAll();
}
@RequestMapping(method = RequestMethod.GET, path = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Movie findOne(@PathVariable("id") String movieId) {
return service.findOne(movieId);
}
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Movie create(@RequestBody Movie movie) {
return service.create(movie);
}
@RequestMapping(method = RequestMethod.PUT, path = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Movie update(@PathVariable("id") String empId, @RequestBody Movie emp) {
return service.update(empId, emp);
}
@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
public void delete(@PathVariable("id") String empId) {
service.delete(empId);
}
试试这种方式。
我们没有建议返回类型等于void,您可以将状态类型返回到页面,以确保操作成功与否