弹簧@Controller和@RestController注释之间的区别.
可以@Controller注解同时用于Web MVC框架和REST的应用程序?
如果是,我们如何区分它是Web MVC还是REST应用程序.
我想有一个双向JSON到Java序列化
我正在成功使用Java到JSON到JQuery路径......(@ResponseBody)例如
@RequestMapping(value={"/fooBar/{id}"}, method=RequestMethod.GET)
public @ResponseBody FooBar getFooBar(
@PathVariable String id,
HttpServletResponse response , ModelMap model) {
response.setContentType("application/json");
...
}
Run Code Online (Sandbox Code Playgroud)
在我使用的JQuery中
$.getJSON('fooBar/1', function(data) {
//do something
});
Run Code Online (Sandbox Code Playgroud)
这很有效(例如注释工作已经完成,感谢所有的回答者)
但是,如何执行反向路径:使用RequestBody将JSON序列化为Java对象?
无论我尝试什么,我都无法得到这样的东西:
@RequestMapping(value={"/fooBar/save"}, method=RequestMethod.POST)
public String saveFooBar(@RequestBody FooBar fooBar,
HttpServletResponse response , ModelMap model) {
//This method is never called. (it does when I remove the RequestBody...)
}
Run Code Online (Sandbox Code Playgroud)
我已经正确配置了Jackson(它在出路时序列化)并且当然我将MVC设置为注释驱动
我如何使其工作?它有可能吗?或者Spring/JSON/JQuery是单向的(out)?
更新:
我改变了杰克逊的设定
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- Bind the return value of the Rest …Run Code Online (Sandbox Code Playgroud) 有了多个消耗和生成的Spring控制器application/json,我的代码充满了长注释,如:
@RequestMapping(value = "/foo", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
Run Code Online (Sandbox Code Playgroud)
有没有办法生成一个"复合/继承/聚合"注释,其默认值为consumes和produces,这样我就可以编写如下内容:
@JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)
我们如何定义@JSONRequestMapping上面的内容?注意value并method传入就像@RequestMapping,也可以传入consumes或produces默认不适合.
我需要控制我要回来的东西.我想要produces/ consumesannotation-methods,以便获得适当的Content-Type头文件.
我尝试在Spring MVC中向我的控制器发出一个AJAX查询.
我的行动代码是:
@RequestMapping(value = "events/add", method = RequestMethod.POST)
public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){
//some code
}
Run Code Online (Sandbox Code Playgroud)
我的Ajax查询是:
$.ajax({
type: "POST",
url:url,
contentType: "application/json",
data: {
start_date: scheduler.getEvent(id).start_date,
end_date: scheduler.getEvent(id).end_date,
text: scheduler.getEvent(id).text,
userId: userId
},
success:function(result){
//here some code
}
});
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误:
必需的字符串参数''start_date不存在
为什么?据我所知,我提出了它(@RequestParam(value = "start_date") String start_date
UDP
现在我给404我的类来获取数据
public class EventData {
public String end_date;
public String start_date;
public String …Run Code Online (Sandbox Code Playgroud) 我有SpringBoot应用程序与该依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我在我的控制器上有一个方法如下:
@RequestMapping(value = "/liamo", method = RequestMethod.POST)
@ResponseBody
public XResponse liamo(XRequest xRequest) {
...
return something;
}
Run Code Online (Sandbox Code Playgroud)
我通过AJAX从我的HTML发送一个JSON对象,其中包含一些XRequest类型对象的字段(它是一个没有任何注释的普通POJO).但是我的JSON没有在我的控制器方法中构造成对象,并且它的字段为空.
我错过了我的控制器的自动反序列化?