我正在尝试使用2.0.5.RELEASE版本的Spring Boot构建RESTful API。下面是我的控制器:
// Just for test
@RestController
public class LoginController {
@RequestMapping(value = "/user/login",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> login(@RequestParam(name = "username") String username,
@RequestParam(name = "password") String password) {
ResponseEntity<RESTResponse> response = null;
if(username.equals("123") && password.equals("123")){
// success
response = new ResponseEntity<>(RESTResponse.generateResponse(
null, "successful", "Log in successfully."), HttpStatus.OK);
} else {
// failed
response = new ResponseEntity<>(RESTResponse.generateResponse(
null, "failed", "Your username or password is incorrect."), HttpStatus.OK);
}
return response;
}
}
这是Spring MVC配置类:
@Configuration
public class MyMvcConfig implements WebMvcConfigurer{
/**
* CORS configuration
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(ALL)
.allowedMethods(ALL)
.allowedHeaders(ALL)
.allowCredentials(true);
}
}
控制器应该响应JSON数据,我用Postman测试了控制器,控制器可以接收请求参数并正常工作,但Postman得到了一个奇怪的响应:
{
"timestamp": "2018-09-16T05:55:14.860+0000",
"status": 406,
"error": "Not Acceptable",
"message": "Could not find acceptable representation",
"path": "/api/user/login"
}
有人能帮忙吗?
4条答案
按热度按时间l0oc07j21#
可以实现Filter接口
并设置header中的所有方法都可以接受
ikfrs5lh2#
请确保在postman标题中使用
Accept: application/json
。如果完成上述操作,则尝试在方法签名中添加
consumes= MediaType.APPLICATION_JSON_VALUE
和products。6kkfgxo03#
请确保您有这些jar并在postman头中使用Header
Accept: application/json
olhwl3o24#
我也遇到了同样的问题
1.添加jackson
1.使用@JsonInclude(JsonInclude.Include.NON_NULL)@JsonIgnoreProperties(忽略未知= true)实作
1.可序列化
但在dto中缺少@Getter。