编辑:当前仍将此作为错误获取:2019-08-08 15:12:50.876警告3164---[io-8080-exec-10]。w、 s.m.s.DefaultHandlerExceptionResolver:已解析[org.springframework.web.HttpMediaTypeNotSupportedException:内容类型“”不受支持]
我真的不知道为什么它说内容类型不支持...它也可以从postman工作,但不显示xml,只显示json,即使我选择了应用程序/xml作为头类型
我试图让我的基本SpringREST服务用XML响应。我已经注释了所有元素,并放置了应该接受XML的标题。由于某些原因,我的客户端仍然收到错误“不支持的媒体类型”。
我试着把@响应体
放在员工服务中的方法旁边,但它最终给了我一个不同的错误,那就是:[org.springframework.web.HttpMediaTypeNotSupport dExc0019: Content type"不支持]
受雇者JAVA
public class Employee implements Serializable {
@XmlAttribute
private String id;
@XmlElement
private String name;
@XmlElement
private String description;
//private List<Team> teams;
public Employee() {
super();
}
public Employee(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
//this.teams = teams;
}
@XmlAttribute
public String getId() {
return id;
}
@XmlAttribute
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getDescription() {
return description;
}
// public List<Team> getTeam() {
// return teams;
// }
@XmlElement
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return String.format(
"employee [id=%s, name=%s, description=%s]", id,
name, description);
}
}
雇员控制员。JAVA
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping(path = "/employees", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public @ResponseBody HashMap<String, Employee> retrieveEmployees() {
return employeeService.retrieveAllEmployees();
}
@GetMapping(path = "/employees/{employeeId}",produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public @ResponseBody Employee retrievebyId(@PathVariable String employeeId) {
return employeeService.retrieveEmployee(employeeId);
}
@PostMapping(path="/employees")
public ResponseEntity<Void> registeremployee(@RequestBody Employee newemployee) {
Employee employee = employeeService.addEmployee(newemployee.getId(),newemployee.getName(), newemployee.getDescription());
if (employee == null)
return ResponseEntity.noContent().build();
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(
"/{id}").buildAndExpand(employee.getId()).toUri();
return ResponseEntity.created(location).build();
}
}
员工ervice.java
@Component
public class EmployeeService {
static HashMap<String, Employee> employees = new HashMap<>();
static {
//Initialize Data
Team team1 = new Team("t1", "Java team", "Java Dev Team");
Employee Joe = new Employee("employee1", "Joe Smith","Human Resources");
Employee Bob = new Employee("employee2", "Bob Jones",
"Developer");
employees.put("employee1", Joe);
employees.put("employee2", Bob);
}
public HashMap<String, Employee> retrieveAllEmployees() {
return employees;
}
public Employee retrieveEmployee(String employeeId) {
return employees.get(employeeId);
}
//private SecureRandom random = new SecureRandom();
public Employee addEmployee(String id, String name, String description) {
//String randomId = new BigInteger(130, random).toString(32);
Employee employee = new Employee(id, name, description);
employees.put(id, employee);
return employee;
}
}
RestClient。JAVA
public class RestClient {
public static void getJsonEmployee(String id) throws JSONException, IOException {
String uri = "http://localhost:8080/employees/" + id;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = restTemplate.headForHeaders(uri);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
String result = restTemplate.getForObject(uri, String.class);
System.out.println(result);
}
public static void postJsonEmployee(String id, String name, String description) {
final String uri = "http://localhost:8080/employees/";
Employee newemp = new Employee(id, name, description);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = restTemplate.headForHeaders(uri);
httpHeaders.setContentType(MediaType.APPLICATION_XML);
Employee result = restTemplate.postForObject( uri, newemp, Employee.class);
httpHeaders.setContentType(MediaType.APPLICATION_XML);
}
public static void main(String[] args) throws IOException, JSONException {
System.out.println("GET or POST?");
BufferedReader getpost = new BufferedReader(new InputStreamReader(System.in));
String selection = getpost.readLine();
switch(selection) {
case "GET":
System.out.println("Type in the employee's ID");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String employeeid = reader.readLine();
getJsonEmployee(employeeid);
break;
case "POST":
System.out.println("Type in the employee's ID");
Scanner scan = new Scanner(System.in);
String newid = scan.nextLine();
System.out.println("Type in the employee's name");
String newname = scan.nextLine();
System.out.println("Type in the employee's description");
String newdesc = scan.nextLine();
postJsonEmployee(newid, newname, newdesc);
break;
}
}
}
10:00:37.986[主]调试组织。springframework。网状物客户RestTemplate-HTTP头http://localhost:8080/employees/
10:00:38.293[主]调试组织。springframework。网状物客户RestTemplate-响应415线程“main”中不支持的_媒体_类型异常
org.springframework.web.client.HttpClientError异常$Unsupport tedMediaType: 415 null atorg.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:95)atorg.springframework.web.client.DefaultResseErrorHandler.handleError(DefaultResseErrorHandler.java:122)atorg.springframework.web.client.DefaultResseErrorHandler.handle错误(默认响应andler.java:102)在org.springframework.web.client.响应错误andler.handle错误(响应错误andler.java:63)在org.springframework.web.client.恢复emplate.handle响应(恢复emplate.java:778)在org.springframework.web.client.RestTemplate.do在org. springframework. web. client上执行(RestTemplate.java:736)在org.springframework.web.client.RestTemplate执行(RestTemplate. java: 670)。在com. client. clientModel.在com. client. clientModel.恢复. main(恢复. java: 104)
波姆。xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>employee-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>employee-model</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
你能试试下面的代码吗。我只修改了getJsonEmployee()方法。
public static void getJsonEmployee(String id) throws JSONException, IOException {
String uri = "http://localhost:8080/employees/" + id;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity entity = new HttpEntity(headers)
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
String result = response.getBody();
System.out.println(result);
}
此外,我看到你的雇员类生成XML是不正确的,我在下面提供了建议的雇员类和一个主要方法,这样你就可以运行来测试XML结构的生成。
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@XmlRootElement
public class Employee implements Serializable {
private String id;
private String name;
private String description;
// private List<Team> teams;
public Employee() {
super();
}
public Employee(String id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
// this.teams = teams;
}
@XmlAttribute
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return String.format("employee [id=%s, name=%s, description=%s]", id, name, description);
}
public static void main(String[] args) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Employee emp = new Employee("id1", "name-1", "desc-1");
marshaller.marshal(emp, System.out);
}
}
Http状态代码系列4xx表示客户端问题。415表示不支持的媒体类型。我不是一个java爱好者,但我以前在ajax调用中遇到过这个问题。为了解决这个问题,我添加了一个header,它是Accept:“*/*”
这个解决方案是针对ajax请求的。对于java,我相信它将是
public static void getJsonEmployee(String id) throws JSONException, IOException {
String uri = "http://localhost:8080/employees/" + id;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = restTemplate.headForHeaders(uri);
headers.add("Accept", "*/*");
String result = restTemplate.getForObject(uri, String.class);
System.out.println(result);
}
405表示您的客户端正在发送错误的请求。您正在客户端中发出HTTP HEAD请求。尝试只是“更新”HttpHeaders。
发送请求后,也无法设置请求的标题。
获取Spring Rest控制器Web服务的以下异常:- org.springframework.web.client.HttpClientError异常: 415org.springframework.web.client.不支持的媒体类型默认响应错误andler.java:91org.springframework.web.client.的错误(默认响应错误andler.java:91)RES
componentDidMount(){
我正在研究Java restful web服务。在测试restful服务时,GET和DELETE方法的响应是正确的,但POST和PUT方法不起作用。有人能帮我吗?我编写了以下代码:
我试图张贴一个文件从邮递员到endpoint我已经创建.但它给我这个错误。我不会在邮递员中传递标题内容类型 415不支持的媒体类型
问题内容: 自数小时以来,我一直在尝试纠正http错误,但它仍显示不支持的页面。我在邮递员中添加标题。 这是我的Java代码 这是我的档案 问题答案: 通过和如何在响应流和请求流之间对对象进行序列化和反序列化。 将会发生的是,将从提供者的注册表中进行搜索,以查找可以处理的媒体类型。如果找不到,则Jersey无法处理该请求,并将发送415不支持的媒体类型。通常,你还应该在服务器端记录一个异常。不知道
我已经创建了一个示例web服务来进行post调用。 我使用的是Jersey JAX-RS和Maven。