我正在开发Spring REST(仅限研究),我仍然坚持以下示例,其结果应为200
,获得404
,因为我有以下代码
Product.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();
}
cart.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
请帮我解决此问题,感谢任何帮助。如果您还需要了解更多信息,请告知我们。
答案 0 :(得分:1)
小思想(即使我还没有测试过),就好像它是,方法注释它们将被解释为相对URL(相对于类级URL)。所以有两件事,你可以试试看。
1)删除类级映射并在方法中放置绝对URL。
@RequestMapping(value = "/cart/rest", method = RequestMethod.POST)
2)在方法级别删除根斜杠,如下所示,保持类级别映射不变。
@RequestMapping(value = "rest", method = RequestMethod.POST)