我创建了简单的添加实体表单,称为AddContact.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<body>
<h2>Contact information</h2>
<form:form method="POST" action="contacts/add" modelAttribute="contact">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="surname">Surname</form:label></td>
<td><form:input path="surname" /></td>
</tr>
<tr>
<td><form:label path="phonenumber">Phonenumber</form:label></td>
<td><form:input path="phonenumber" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
这是REST HANDLER-SERVLET.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="facade.rest" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这是web.xml
的servlet映射<servlet-mapping>
<servlet-name>rest-handler</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
,最后一个控制器:
package facade.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import domain.ContactDO;
import service.IContactService;
@Controller
public class ContactController {
private IContactService contactService;
@Required
public void setContactService(IContactService contactService) {
this.contactService = contactService;
}
@RequestMapping(value = "/contacts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<List<ContactDO>> getContacts() {
List<ContactDO> contacts = contactService.load();
if(contacts != null) {
return new ResponseEntity<>(contacts, HttpStatus.OK);
}
else
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
@RequestMapping(value = "/contacts/{id:.*}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<ContactDO> getContact(@PathVariable Integer id) {
ContactDO contact = contactService.loadContact(id);
if(contact != null) {
return new ResponseEntity<>(contact, HttpStatus.OK);
}
else
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
@RequestMapping(value = "/contacts/add", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact") ContactDO contact, ModelMap model) {
model.addAttribute("name", contact.getName());
model.addAttribute("surname", contact.getSurname());
model.addAttribute("phonenumber", contact.getPhonenumber());
//contact.setId(2);
contactService.store(contact);
return "contacts";
}
}
当我尝试从http://localhost访问:8080/pb/contacts/add(pb是战争的名称(时,我会得到http400。logs告诉我我正在尝试执行get请求。
当我尝试从AT访问时 http://localhost:8080/pb/contacts/add(pb是战争的名称(我得到http 400.日志告诉我我正在尝试执行get请求。
是对的!您正在使用GET请求尝试/contacts/add
,并且请求处理程序无法映射您的请求,因为/contacts/add
仅通过POST请求访问。查看您的方法定义:
@RequestMapping(value = "/contacts/add", method = RequestMethod.POST)
public String addContact(@ModelAttribute("contact") ContactDO contact, ModelMap model) {
}
您需要一个邮政请求才能达到您的方法。
编辑:我想您想使用/contacts/add
方法编辑联系人。如果是这种情况,请首先致电/contacts/{id:.*}
,其中id
是您的ID。然后,您可以通过提交编辑来进行编辑并发送帖子。这就是您到达/contacts/add
页面的方式。
如果您不知道任何id
,请首先致电http://localhost:8080/pb/联系人为您的联系人。您的意愿(希望(找到您的联系人进行编辑。
编辑**:在您的休息客户端,您是否尝试过将请求类型更改为 POST
?