我正在使用简单的Spring MVC REST CURD操作。
我可以访问GET
和DELETE
方法。
但是,POST
& PUT
无效。
在向api网址发布数据时,邮递员显示415 Unsupported Media Type Error
服务器拒绝了此请求,因为请求实体的格式不受所请求方法所请求资源的支持。
伙计们,请帮助解决这个问题
我只是尝试使用git的代码
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.vodafone.crud.dto.CustomerDto;
import com.vodafone.crud.model.Customer;
import com.vodafone.crud.model.Status;
import com.vodafone.crud.service.DataServices;
/**
* @author Geeth
*
*/
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
DataServices dataServices;
static final Logger logger = Logger.getLogger(CustomerController.class);
/**
* Use to add a new customer to database.
*
* @param dto
* Customer data transfer object
* @return Whether success of the operation
*, consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
public @ResponseBody Status addCustomer(@RequestBody CustomerDto dto) {
logger.debug("Delegating to service to return Added Customer");
try {
dataServices.addEntity(dto);
logger.debug("Customer added Successfully !");
return new Status(1, "Customer added Successfully !");
} catch (Exception e) {
logger.warn("Customer wasn't added !");
return new Status(0, e.toString());
}
}
/**
* Use to update an existing customer to database.
*
* @param dto
* Customer data transfer object
* @return Whether success of the operation
*/
@RequestMapping(value = "/update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Status updateCustomer(@RequestBody CustomerDto dto) {
logger.debug("Delegating to service to return updated Customer");
try {
dataServices.updateEntity(dto);
return new Status(1, "Customer updated Successfully !");
} catch (Exception e) {
logger.warn("Customer wasn't updated !");
return new Status(0, e.toString());
}
}
/**
* Use to retrieve an exiting customer
*
* @param id
* Unique identifier of the particular customer.
* @return Fetched customer
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody CustomerDto getCustomer(@PathVariable("id") long id) {
logger.debug("Delegating to service to return Customer " + id);
CustomerDto dto = null;
try {
dto = dataServices.getEntityById(id);
} catch (Exception exe) {
logger.debug("Customer wasn't fetched " + id);
logger.error(exe.getMessage());
}
return dto;
}
/**
* Use to retrieve all existing customers
*
* @return List of fetched customers
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public @ResponseBody List<CustomerDto> getCustomers() {
logger.debug("Delegating to service to return all Customers");
List<CustomerDto> customerList = null;
try {
customerList = dataServices.getEntityList();
} catch (Exception exe) {
logger.debug("Customers fetching was failed");
logger.error(exe.getMessage());
}
return customerList;
}
/**
* Use to delete an existing customer
*
* @param id
* Unique identifier of the particular customer.
* @return Whether success of the operation
*/
@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
public @ResponseBody Status deleteCustomer(@PathVariable("id") long id) {
logger.debug("Delegating to service to delete Customer" + id);
try {
dataServices.deleteEntity(id);
return new Status(1, "Customer deleted Successfully !");
} catch (Exception exe) {
logger.debug("Customer wasn't deleted " + id);
return new Status(0, exe.toString());
}
}
}
Customer.java(模型/实体类):
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "customer")
//@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "phone")
private String phoneNumber;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vodafone.crud</groupId>
<artifactId>Vodafone</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.0.8.RELEASE</spring.version>
<hibernate.version>4.3.5.Final</hibernate.version>
<log4j.version>1.2.17</log4j.version>
<jdk.version>1.8</jdk.version>
<context.path>SpringRestCrud</context.path>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>