Spring MVC 3.1 RESTful Controller

标签 spring rest jakarta-ee

我即将编写一个 Spring MVC Controller 来服务/接收 HTML 表单和 JSON。最好的方法似乎是使用 RESTful Controller ,但作为我编写的第一个 Controller ,我想做得正确!

是否有可能有一种方法,如果是 HTML 请求,则返回由 InternalResourceViewResolver 呈现的 View ,如果是 ajax 请求,则返回要呈现为 JSON 的实体?

更新也是如此,您能否编写一个 Controller 方法,根据内容类型接受从传入 JSON 转换的对象或来自 HTML 表单的 @Valid 对象?

我认为您必须能够,否则为什么要使用 sf taglib 表单元素支持 HTML 表单中的 DELETE 和 PUT 操作?只是似乎无法在任何地方找到如何执行此操作的解释!

干杯! 网络功能虚拟化

最佳答案

我会尝试一下。

这是我的 Configuration 类中的内容:

@Bean(name = "viewResolver")
public ContentNegotiatingViewResolver viewResolver() {
    final ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
    contentNegotiatingViewResolver.setOrder(1);
    contentNegotiatingViewResolver.setFavorPathExtension(true);
    contentNegotiatingViewResolver.setFavorParameter(true);
    contentNegotiatingViewResolver.setIgnoreAcceptHeader(false);
    final Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", "application/x-json");
    mediaTypes.put("json", "text/json");
    mediaTypes.put("json", "text/x-json");
    mediaTypes.put("json", "application/json");
    mediaTypes.put("xml", "text/xml");
    mediaTypes.put("xml", "application/xml");
    contentNegotiatingViewResolver.setMediaTypes(mediaTypes);
    final List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(jsonView());
    defaultViews.add(xmlView());
    contentNegotiatingViewResolver.setDefaultViews(defaultViews);
    return contentNegotiatingViewResolver;
}

@Bean(name = "xStreamMarshaller")
public XStreamMarshaller xStreamMarshaller() {
    return new XStreamMarshaller();
}

@Bean(name = "xmlView")
public MarshallingView xmlView() {
    final MarshallingView marshallingView = new MarshallingView(xStreamMarshaller());
    marshallingView.setContentType("application/xml");
    return marshallingView;
}

@Bean(name = "jsonView")
public MappingJacksonJsonView jsonView() {
    return new MappingJacksonJsonView();
}

这是 Controller 中的内容。

@RequestMapping(value = { "/pets" }, method = RequestMethod.GET)
public String list(Model model) {
    model.addAttribute("pets", petRepository.findAll());
    return "pets/list";
}

@RequestMapping(value = { "/pets" }, method = RequestMethod.POST)
public String create(@Valid @RequestBody Pet pet, Model model) {
    petRepository.save(pet);
    return "redirect:pets/read/" + pet.getId();
}

@RequestMapping(value = { "/pets/{petId}" }, method = RequestMethod.GET)
public String read(@PathVariable Integer petId, Model model) {
    model.addAttribute("pet", petRepository.findOne(petId));
    return "pets/read";
}

@RequestMapping(value = { "/pets" }, method = RequestMethod.PUT)
public String update(@Valid @RequestBody Pet pet, Model model) {
    petRepository.save(pet);
    return "redirect:pets/read/" + pet.getId();
}

@RequestMapping(value = { "/pets/{orderId}" }, method = RequestMethod.DELETE)
public void delete(@PathVariable Integer petId, Model model) {
    petRepository.delete(petId);
}

根据我的经验,您可以将 HTML 表单或 JSON 对象作为 @RequestBody 提交。尝试一下。

关于Spring MVC 3.1 RESTful Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12798488/

相关文章:

java - 无法将 String 类型的值转换为 MethodSecurityMetadataSource (Spring 3.1)

c# - Transactionscope 和 webHttpBinding

java - Spring RMI错误java.lang.ClassNotFoundException : org. springframework.remoting.rmi.RmiInvocationHandler

java - 运行两个并行事务

java - Wildfly 随机被杀死

java - Spring 集成 JMS 在入站和出站中发布订阅

hibernate - @autowired 注释问题,不在类中注入(inject) bean,使用 Spring3.0, hibernate

java - Spring-boot swagger 在 io.swagger.models.parameters.Parameter 上抛出 java.lang.ClassNotFoundException

java - 如何避免捕获异常后返回null?

rest - Web UI 到一个 Restful 界面,好主意吗?