首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring MVC REST映射在postman中返回错误404

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

Stack Overflow用户
提问于 2016-12-05 12:58:41
回答 1查看 1.3K关注 0票数 1

我在spring REST上工作(仅限学习),我被困在下面的例子中,结果应该是200,获得404,因为我有以下代码

Product.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
    }

cart.java

代码语言:javascript
复制
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

代码语言:javascript
复制
@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

代码语言:javascript
复制
{
    "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配置文件

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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请帮助我解决这个问题,任何帮助是感激的。如果您需要了解更多信息,请让我知道。

EN

回答 1

Stack Overflow用户

发布于 2016-12-05 15:06:36

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

1)移除类级映射,并在方法中放置绝对url。

代码语言:javascript
复制
@RequestMapping(value = "/cart/rest", method = RequestMethod.POST)

2)删除方法级别的根斜杠,如下所示,并保持类级别映射的原样。

代码语言:javascript
复制
@RequestMapping(value = "rest", method = RequestMethod.POST)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40967376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档