[Ruimo.com] 프런트 엔드 ajax 요청 인터페이스, 백엔드 인터페이스가 실제로 액세스되었으며 데이터가 데이터베이스에 성공적으로 삽입되었습니다. 그러나 프런트 엔드는 404를보고했습니다.

정상적인 상황에서 프런트 엔드에서 보고된 404 오류는 인터페이스를 찾을 수 없음을 의미합니다. 그러나이 경우 백엔드 인터페이스가 성공적으로 실행되었지만 프런트엔드는 여전히 오류 404를 보고합니다. 오류는 다음과 같습니다.

그러나 백엔드 인터페이스는 정상적으로 실행되고 데이터베이스에 레코드가 추가되었습니다.

프런트엔드 코드:

 this.$http.post("/shop/settledIn",para).then((res) => {
                                if(res.data.success){
                                    this.$message({
                                        message: '操作成功!',
                                        type: 'success'
                                    });
                                    //重置表单
                                    this.$refs['shopForm'].resetFields();
                                    //跳转登录页面
                                    this.$router.push({ path: '/login' });
                                }
                                else{
                                    this.$message({
                                        message: res.data.msg,
                                        type: 'error'
                                    });
                                }
                            });

백엔드 코드:

package com.rk.pethome.controller;
import com.rk.pethome.domain.Shop;
import com.rk.pethome.service.IShopService;
import com.rk.pethome.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@Controller
@RequestMapping("/shop")
public class ShopController {
 
    @Autowired
    private IShopService shopService;
 
    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            shopService.settledIn(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
}

이 오류를 찾는 데 오랜 시간이 걸렸습니다. 처음에는 프론트엔드 오류인 줄 알았습니다. 사실 백엔드 인터페이스의 주석이 잘못 사용되었습니다. 여기에서 뷰 대신 객체가 반환됩니다. @ @Controller 주석 대신 RestController 주석을 사용해야 합니다 .

@Controller와 @RestController의 차이점:

@Controller는 뷰 파서입니다. 즉, Return은 뷰, 즉 jsp 또는 html 페이지를 반환합니다.

데이터 json, xml 등을 반환할 경우 해당 메소드에 @ResponseBody 애노테이션을 추가해야 한다.

@RestController는 @Controller와 @ResponseBody 애노테이션의 조합으로 json 데이터를 반환하기 위해 메소드 앞에 @ResponseBody 애노테이션을 추가할 필요는 없지만 @RestController 애노테이션을 사용하면 jsp와 html 페이지를 반환할 수 없고 뷰가 파서가 jsp.html 페이지를 구문 분석할 수 없습니다.

Supongo que te gusta

Origin blog.csdn.net/rrmod/article/details/128905648
Recomendado
Clasificación