基于x-www-form-urlencoded与Spring @Controller的问题的答案
我写了下面的@Controller方法
@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST
, produces = {"application/json", "application/xml"}
, consumes = {"application/x-www-form-urlencoded"}
)
public
@ResponseBody
Representation authenticate(@PathVariable("email") String anEmailAddress,
@RequestBody MultiValueMap paramMap)
throws Exception {
if(paramMap == null || paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
}
Run Code Online (Sandbox Code Playgroud)
请求失败并出现以下错误
{
"timestamp": 1447911866786,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/users/usermail%40gmail.com/authenticate"
}
Run Code Online (Sandbox Code Playgroud)
[PS:泽西岛更加友好,但现在考虑到实际限制,现在无法使用它]
我有一个 AngularJS Web 应用程序,看起来它正确地将 JSON 发送到 Tomcat 服务器,但服务器收到空值。这个问题没有帮助,因为我的属性名称已经是小写,并且这个问题没有帮助,因为我已经有了符号@RequestBody。编辑:正如评论中指出的,我实际上没有这个符号。我读错了。
这是有问题的服务器方法:
\n\n@PostMapping(path="/kind/add")\npublic @ResponseBody String addNewKind(Kind kind) throws Exception {\n if (kind.getName() == null) {\n throw new Exception("Name not found.");\n }\n kindRepository.save(kind);\n return "Saved";\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是它期望接收的 Kind 对象:
\n\n@Entity\npublic class Kind {\n\n @Id\n @GeneratedValue(strategy=GenerationType.AUTO)\n private int id;\n private String name;\n @OneToMany(mappedBy="kind")\n private Set<Card> cards;\n\n public int getId() {\n return id;\n }\n\n public void setId(int id) {\n this.id = id;\n }\n\n public String getName() …Run Code Online (Sandbox Code Playgroud) 使用接受JSON请求主体的Spring MVC开发REST Web服务.并进一步处理收到的消息.我使用以下:Eclipse,Tomcat,Spring 3.0.1,Jackson lib,Curl来测试Web服务
`curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"fname":"my_firstname" , "lname":"my_lastname"}' http://localhost:8080/SpringMVC/restful`Run Code Online (Sandbox Code Playgroud)
回国
"Saved person: null null"Run Code Online (Sandbox Code Playgroud)
我的控制器类
import com.samples.spring.Person;
@Controller
public class RestController {
@RequestMapping(value="{person}", method = RequestMethod.POST)
@ResponseBody
public String savePerson(Person person) {
// save person in database
return "Saved person: " + person.getFname() +" "+ person.getLname();
}
Run Code Online (Sandbox Code Playgroud)
我的班级
package com.samples.spring;
public class Person {
public String fname;
public String lname;
public String getFname() {
return fname;
}
public void …Run Code Online (Sandbox Code Playgroud)