我有一個與mongoDB作爲數據庫的Spring啓動應用程序。只有GET Http請求正在工作。對於所有其他方法,我得到的錯誤代碼404(信息不可用)春季啓動RestController總是得到404錯誤代碼(GET Http請求工作正常,但所有其他結果爲錯誤代碼)
應用程序看起來是這樣的:
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
public class ManagementApplication {
public static void main(String[] args) {
SpringApplication.run(ManagementApplication.class, args);
}
}
控制器:
@RestController
@RequestMapping("/user")
public class UserController {
private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<User> findAll() {
LOGGER.info("Finding all User entries");
return userService.findAllUsers();
}
@RequestMapping(value = "{userId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public User findById(@PathVariable(value = "userId") String userId) {
LOGGER.info("Getting the user with the id: " + userId);
return userService.findUserWithId(userId);
}
@RequestMapping(value = "{userId}", method = RequestMethod.DELETE)
public void delete(@PathVariable(value = "userId") String userId) {
LOGGER.info("Deleting the user with the id: " + userId);
userService.deleteUser(userId);
}
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public User create(@RequestBody @Valid User user, BindingResult result) throws Exception {
LOGGER.info("Creating or Updating a user with the following informations: " + user);
if (result.hasErrors()) {
throw new Exception("Error in request");
}
return userService.createOrUpdateUser(user);
}
@RequestMapping(method = RequestMethod.PUT)
public User update(@RequestBody @Valid User user) {
LOGGER.info("Updating the user with the following informations: " + user);
return userService.createOrUpdateUser(user);
}
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public void handleUserNotFound(Exception exception) {
LOGGER.info("An Exception occured in controller " + exception.getMessage());
}
對於除得到我之外的任何其他通話得到以下回應:
"timestamp": "1475149066750",
"status": "404",
"error": "Not Found",
"message": "No message available",
"path": "/user"
請任何人都可以在這幫助我嗎?
您正在使用哪種工具測試GET以外的調用? –
我只是使用其餘的客戶端工具(CocoaRestClient)。結果與捲曲相同 – Wafo
您是否在restclient中設置了合適的標頭 –