Spring mvc 无法读取HTTP消息:org.springframework.HTTP.converter.httpMessageNodeTableException:缺少必需的请求正文:
我已经创建了一个Rest控制器。我正试图通过Chrome中的POSTMAN访问它。但我得到了这个错误: 警告:无法读取HTTP消息:org.springframework.HTTP.converter.httpMessageNodeTableException:缺少必需的请求正文:public org.springframework.HTTP.ResponseEntity com.ukp.controller.Spring4MVCRestController.createUser(com.ukp.model.User,org.springframework.web.util.UriComponentsBuilder) 我不知道原因是什么。请帮我解决这个问题Spring mvc 无法读取HTTP消息:org.springframework.HTTP.converter.httpMessageNodeTableException:缺少必需的请求正文:,spring-mvc,restful-url,Spring Mvc,Restful Url,我已经创建了一个Rest控制器。我正试图通过Chrome中的POSTMAN访问它。但我得到了这个错误: 警告:无法读取HTTP消息:org.springframework.HTTP.converter.httpMessageNodeTableException:缺少必需的请求正文:public org.springframework.HTTP.ResponseEntity com.ukp.controller.Spring4MVCRestController.createUser(com.ukp
package com.ukp.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;
import com.ukp.model.User;
import com.ukp.service.UserService;
@RestController
public class Spring4MVCRestController {
@Autowired
UserService userService; //Service which will do all data retrieval/manipulation work
//-------------------Retrieve All Users--------------------------------------------------------
@RequestMapping(value = "/user/", method = RequestMethod.GET)
public ResponseEntity<List<User>> listAllUsers() {
List<User> users = userService.findAllUsers();
if(users.isEmpty()){
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
//-------------------Retrieve Single User--------------------------------------------------------
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@PathVariable("id") long id) {
System.out.println("Fetching User with id " + id);
User user = userService.findById(id);
if (user == null) {
System.out.println("User with id " + id + " not found");
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<User>(user, HttpStatus.OK);
}
//-------------------Create a User--------------------------------------------------------
@RequestMapping(value = "/user/", method = RequestMethod.POST)
public ResponseEntity<Void> createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {
System.out.println("Creating User " + user.getName());
if (userService.isUserExist(user)) {
System.out.println("A User with name " + user.getName() + " already exist");
return new ResponseEntity<Void>(HttpStatus.CONFLICT);
}
userService.saveUser(user);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(user.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
//------------------- Update a User --------------------------------------------------------
@RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
public ResponseEntity<User> updateUser(@PathVariable("id") long id, @RequestBody User user) {
System.out.println("Updating User " + id);
User currentUser = userService.findById(id);
if (currentUser==null) {
System.out.println("User with id " + id + " not found");
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
}
currentUser.setName(user.getName());
currentUser.setAge(user.getAge());
currentUser.setSalary(user.getSalary());
userService.updateUser(currentUser);
return new ResponseEntity<User>(currentUser, HttpStatus.OK);
}
//------------------- Delete a User --------------------------------------------------------
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public ResponseEntity<User> deleteUser(@PathVariable("id") long id) {
System.out.println("Fetching & Deleting User with id " + id);
User user = userService.findById(id);
if (user == null) {
System.out.println("Unable to delete. User with id " + id + " not found");
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
}
userService.deleteUserById(id);
return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
}
//------------------- Delete All Users --------------------------------------------------------
@RequestMapping(value = "/user/", method = RequestMethod.DELETE)
public ResponseEntity<User> deleteAllUsers() {
System.out.println("Deleting All Users");
userService.deleteAllUsers();
return new ResponseEntity<User>(HttpStatus.NO_CONTENT);
}
}
这是我的SpringMVC4初始值设定项:
package com.ukp.configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMVC4Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {SpringMVC4Configuration.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
最后,我的pom.xml:
4.0.0
com.ukp
Spring4MVCrudRestService
0.0.1-快照
战争
带RestFull演示的SpringMVC
4.3.7.1发布
2.5.3
org.springframework
spring上下文
${spring.version}
org.springframework
春豆
${spring.version}
org.springframework
弹簧网
${spring.version}
org.springframework
SpringWebMVC
${spring.version}
org.springframework
德克萨斯州春季
${spring.version}
com.fasterxml.jackson.core
杰克逊数据绑定
${jackson.version}
javax.servlet
javax.servlet-api
3.1.0
org.apache.maven.plugins
maven编译器插件
3.2
1.8
1.8
org.apache.maven.plugins
maven战争插件
2.4
src/main/webapp
Spring4MVCrudRestService
假的
Spring4MVCrudRestService
错误:
INFO:FrameworkServlet“dispatcher”:初始化在381毫秒内完成
2017年4月3日下午4:39:42 org.apache.catalina.core.StandardContext
信息:已完成重新加载名为[/Spring4MVCrudRestService]的上下文
2017年4月3日下午4:41:26 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver HandleHttpMessageNodeable
警告:无法读取HTTP消息:org.springframework.HTTP.converter.httpMessageNodeTableException:缺少必需的请求正文:public org.springframework.HTTP.ResponseEntity com.ukp.controller.Spring4MVCRestController.createUser(com.ukp.model.User,org.springframework.web.util.UriComponentsBuilder)
2017年4月3日下午4:41:40 org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
我正在尝试通过邮递员访问POST请求:http://localhost:8282/Spring4MVCCRUDRestService/user/
这真的是演示问题的最小完整程序吗?如果你能把这个问题简化为一个小问题,你就更有可能得到高质量的答案。这真的是证明问题的最小完整程序吗?如果你能将其降低到最低限度,你就更有可能得到高质量的答案。
package com.ukp.service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ukp.model.User;
@Service
@Transactional
public class UserServiceImpl implements UserService {
private static final AtomicLong counter = new AtomicLong();
private static List<User> users;
static{
users= populateDummyUsers();
}
public List<User> findAllUsers() {
return users;
}
@Override
public User findById(long id) {
for(User user : users){
if(user.getId() == id){
return user;
}
}
return null;
}
@Override
public User findByName(String name) {
for(User user : users){
if(user.getName().equalsIgnoreCase(name)){
return user;
}
}
return null;
}
@Override
public void saveUser(User user) {
user.setId(counter.incrementAndGet());
users.add(user);
}
@Override
public void updateUser(User user) {
int index = users.indexOf(user);
users.set(index, user);
}
@Override
public void deleteUserById(long id) {
for (Iterator<User> iterator = users.iterator(); iterator.hasNext(); ) {
User user = iterator.next();
if (user.getId() == id) {
iterator.remove();
}
}
}
public static List<User> populateDummyUsers() {
List<User> users = new ArrayList<User>();
users.add(new User(counter.incrementAndGet(),"Sam",30, 70000));
users.add(new User(counter.incrementAndGet(),"Tom",40, 50000));
users.add(new User(counter.incrementAndGet(),"Jerome",45, 30000));
users.add(new User(counter.incrementAndGet(),"Silvia",50, 40000));
return users;
}
@Override
public void deleteAllUsers() {
users.clear();
}
@Override
public boolean isUserExist(User user) {
return findByName(user.getName())!=null;
}
}
package com.ukp.model;
public class User {
private long id;
private String name;
private int age;
private double salary;
public User() {
id = 0;
}
public User(long id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age
+ ", salary=" + salary + "]";
}
}
package com.ukp.configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringMVC4Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {SpringMVC4Configuration.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
package com.ukp.configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.ukp")
public class SpringMVC4Configuration {
}