我写了一个GET和一个POST终点。思想高级REST客户端我可以成功进行GET调用。但是POST端点从REST客户端失败。它提供405方法不允许状态。这里有什么问题?
依赖
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
休息控制器
@RestController
@RequestMapping("/rest/orders")
public class OrderRestController {
@Autowired
private FoodItemService foodItemService;
@RequestMapping(value = "", method = RequestMethod.POST)
public OrderDto addOrder(OrderDto orderDto){
return orderDto;
}
@RequestMapping(value = "", method = RequestMethod.GET)
public OrderDto getOrder(){
FoodItem foodItem = foodItemService.findOne(1, Boolean.TRUE);
OrderDto orderDto = new OrderDto();
orderDto.setRoomId(23);
Map<FoodItem, Integer> orders = new HashMap<FoodItem, Integer>();
orders.put(foodItem, 3);
return orderDto;
}
}
答案 0 :(得分:0)
我尝试使用您的代码,它适用于我。我不确切知道您使用Spring4为RESTful服务使用哪种Spring配置和格式(JSON)。
这是我使用的maven配置:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
以下是文件web.xml
中的配置:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
以下是我在文件springmvc-servlet.xml
中用于配置bean / content转换的配置:
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
我可以执行这样的请求:
POST http://localhost:8080/restapp/rest/orders
{
"orderDto": {
"id": "some id",
"name": "some name",
"roomId": 23
}
}
200 OK
对于班级OrderDto
,我使用假结构和数据; - )
希望它可以帮到你, 亨利
答案 1 :(得分:0)
问题不是杰克逊。问题是启用了csrf。应该进一步研究禁用csrf和安全性,但目前禁用它是解决方案。