java - Spring MVC REST 映射在 postman 中返回错误 404

标签 java spring rest spring-mvc

我正在研究 spring REST(仅限研究),我被困在以下示例中,结果应该是 200,得到 404 因为我有以下代码

产品.java

public class Product {
    protected String productId;
    protected String name;
    protected BigDecimal unitPrice;
    protected String description;
    protected String manufacturer;
    protected String category;
    protected long unitsInStock;
    protected long unitsInOrder;
    protected boolean discontinued;
    protected String condition;

cartItem.java

public class CartItem {

    protected Product product;
    protected int quantity;
    protected BigDecimal totalPrice;

    public CartItem() {
    }

    public CartItem(Product product) {
        this.product = product;
        this.quantity = 1;
        this.totalPrice = product.getUnitPrice();
    }

购物车.java

public class Cart {

    protected String cartId;
    protected Map<String, CartItem> cartItems;
    protected BigDecimal grandTotal;

    public Cart() {
        cartItems = new HashMap<>();
        grandTotal = new BigDecimal(0);
    }

cartController.java

@Controller
@RequestMapping("/cart")
public class CartRestController {

    @Autowired
    protected CartService cartService;

    @Autowired
    protected ProductService productService;

    @RequestMapping(value = "/rest", method = RequestMethod.POST)
    public @ResponseBody Cart create(@RequestBody Cart cart) {
        return cartService.create(cart);

    }

    @RequestMapping(value = "/{cartId}", method = RequestMethod.GET)
    public @ResponseBody Cart read(@PathVariable(value = "cartId") String cartId) {
        return cartService.read(cartId);
    }

这是我的 json

{
    "cartId": "1234",
    "cartItems": {
        "P1234": {
            "product": {
                "productId": "P1234",
                "name": "iPhone 5s",
                "unitPrice": 500,
                "description": "Apple iPhone 5s smartphone with 4.00-inch 640 x1136 display and 8 - megapixel rear camera ",
                "manufacturer": "Apple",
                "category": "Smart Phone",
                "unitsInStock": 1000,
                "unitsInOrder": 0,
                "discontinued": false,
                "condition": "NEW"
            },
            "quantity": 1,
            "totalPrice": 500
        }
    },
    "grandTotal": 500
}

编辑: Spring配置文件

<mvc:annotation-driven enable-matrix-variables="true" />
    <context:component-scan base-package="com.local.domain" />

    <mvc:resources location="/resource/" mapping="/resource/**" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp" />
        <property name="contentType" value="UTF-8" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="message" />
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="10240000" />
    </bean>

<!--    <bean id="contentNegotiationViewResolver" -->
<!--        class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> -->
<!--        <property name="defaultContentType" value="text/html" /> -->
<!--        <property name="ignoreUnknownPathExtensions" value="true" /> -->
<!--        <property name="ignoreAcceptHeader" value="true" /> -->
<!--        <property name="useJaf" value="true" /> -->
<!--    </bean> -->

<!--    <mvc:view-resolvers> -->
<!--        <mvc:content-negotiation> -->
<!--            <mvc:default-views> -->
<!--                <bean -->
<!--                    class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> -->
<!--                <bean -->
<!--                    class="org.springframework.web.servlet.view.xml.MappingJackson2XmlView" /> -->
<!--            </mvc:default-views> -->
<!--        </mvc:content-negotiation> -->
<!--    </mvc:view-resolvers> -->

</beans>

web.xml

<servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/webcontext/springDispatcherServlet-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

处理此请求后,我收到错误 404 请帮助我解决此问题,我们将不胜感激。如果您需要了解更多信息,请告诉我。

最佳答案

小想法(尽管我还没有测试过),好像是,方法注释它们将被解释为相对 URL(相对于类级别 URL)。所以有两件事,你可以尝试和检查。

1) 删除类级别映射并将绝对 url 放置在方法中。

@RequestMapping(value = "/cart/rest", method = RequestMethod.POST)

2)删除方法级别的根斜线,如下所示,并保持类级别映射不变。

@RequestMapping(value = "rest", method = RequestMethod.POST)

关于java - Spring MVC REST 映射在 postman 中返回错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40967376/

相关文章:

java - 带有属性值请求映射的 Spring Boot REST Controller 测试

jquery - 使用 JQuery PATCH 进行部分更新

java - 有没有半自动的方法来为 i18n 执行字符串提取?

spring - 保存后的 JPA 查询不返回数据库生成的字段

mysql - Cleardb (mySQL) 与 Heroku (spring) 的连接问题

java - Spring Boot 2(Spring Batch 应用程序)无法启动。失败并出现 BeanCreationException : Error creating bean with name 'h2Console'

java - 如何管理休息服务中的 session ?

java - 无法连接数据库

java - Google FireStore 支持的自定义对象有限制吗?

java - 个人所有网络服务 api 的请求计数器