在我学习SpingMVC的过程中我遇到了一些问题
HTTP Status 415 – Unsupported Media Type
Type Status Report
Message Content-Type 'application/json;charset=UTF-8' is not supported.
Description The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.
我尝试使用Apifox访问地址http://localhost:8080/user/userSettingJSON
时候出现了上面的警告,我不知道是什么原因
我的Java代码
package com.wujingkai.user_controller;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.*;
//@RestController
@Controller
@RequestMapping("/user")
public class UserController {
/**
* 支持多个路径可以在value中使用 {"path1", "path2"}
* 支持模糊匹配 * | **
* /user/* 意味着 可以是任意的字符 /user/a /user/b "/user/a/b是不可以的"
* /user/** 意味着 /user/a /user/b /user/a/b/c/d
* 可以直接提取共用的 路径到 类上, 最后的结果路径为 类地址 + 方法地址
* <p>
* 如果RequestMapping不写value默认是类上的地址
* 如果请求方式不写任何请求地址都可以
*/
@RequestMapping
@ResponseBody
public String index() {
System.out.println("Hello World");
return "You Can Login into our website." + "<br/>" +
"Please <a href='/user/login'>Click This Link To Login</a>";
}
@RequestMapping(value = "/login")
@ResponseBody
public String login(@RequestParam String userName, @RequestParam String password, @RequestParam(defaultValue = "1", required = false) int isAgree) {
// account 可传递
// page 可以不传递
if(this.userNames.contains(userName) && password.equals("123") && isAgree == 1)
return "Login Successfully <a href='https://www.baidu.com'>Click this link to redirect your dashboard.</a>";
else
return "Login Failed";
}
@RequestMapping(value = "register", method = RequestMethod.GET)
@ResponseBody
public String register(@RequestParam List<String> userNames) {
this.userNames.addAll(userNames);
return userNames.toString();
}
/**
* 用户设置 传递一个用户的实体类使用param的方式去接收
* @param user 用户实体类
* @return 是否设置成功
*/
// @RequestMapping(value = "userSetting", method = RequestMethod.GET)
// public String userSetting(User user) {
// System.out.println("user = " + user);
// return "Setting is successfully!";
// }
/**
* 使用JSON的方式接受,但是Java不支持Json方式接口只能使用插件完成
* 1. 在handlerAdapter 方法中配置json解析器
* 2. 下载json处理的包
* {
* name: x,
* age: x,
* salary: x
* }
*/
@Data
private class Person {
private String name;
private int age;
private String gender;
}
@RequestMapping(value = "userSettingJSON")
@PostMapping
@ResponseBody
public String userSettingJSON(@RequestBody Person person) {
System.out.println(person);
return "Setting is successfully!";
}
}
Java配置类
package com.wujingkai.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@EnableWebMvc // 配置JSON 解析器
@Configuration
@ComponentScan("com.wujingkai.user_controller")
public class Config {
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
return new RequestMappingHandlerMapping();
}
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
return new RequestMappingHandlerAdapter();
}
}
Maven子工程
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>SpringMVC</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-mvc-base-02</artifactId>
<packaging>war</packaging>
<name>Archetype - spring-mvc-base-02</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.0</version>
</dependency>
</dependencies>
</project>
依赖已经确认没有问题