Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringREST3支持XML和JSON_Java_Rest_Spring Mvc_Xstream_Jackson - Fatal编程技术网

Java SpringREST3支持XML和JSON

Java SpringREST3支持XML和JSON,java,rest,spring-mvc,xstream,jackson,Java,Rest,Spring Mvc,Xstream,Jackson,如果我们使用SpringMVC开发REST,它将支持XML和JSON数据。我已经在我的spring配置bean中编写了ContentNegotiationViewReserverappservlet.xml 我的spring REST控制器是: @Controller @RequestMapping("/rest/customers") class CustomerRestController { protected Log log = LogFactory.getLog(Customer

如果我们使用SpringMVC开发REST,它将支持XML和JSON数据。我已经在我的spring配置bean中编写了ContentNegotiationViewReserver
appservlet.xml


我的spring REST控制器是:

@Controller
@RequestMapping("/rest/customers")
class CustomerRestController {

protected Log log = LogFactory.getLog(CustomerRestController.class);

@RequestMapping(method = POST)
@ResponseStatus(CREATED)
public void createCustomer(@RequestBody Customer customer,
        HttpServletResponse response) {

    log.info(">>>" + customer.getName());
    response.setHeader("Location", String.format("/rest/customers/%s",
            customer.getNumber()));
}


@RequestMapping(value = "/{id}", method = GET)
@ResponseBody
public Customer showCustomer(@PathVariable String id) {
    Customer c = new Customer("0001", "teddy", "bean");
    return c;
}


@RequestMapping(value = "/{id}", method = PUT)
@ResponseStatus(OK)
public void updateCustomer(@RequestBody Customer customer) {
    log.info("customer: " + customer.getName());
}
我在我的客户域类中设置了
@XStreamAlias(“客户”)
注释。 但是当我尝试访问
http://localhost:8080/rest/customers/teddy.xml
它总是响应JSON数据

我在我的customer域类中设置了
@XmlRootElement(name=“customer”)
注释。 但是当我尝试访问
http://localhost:8080/rest/customers/teddy.json
它总是响应XML数据

有什么不对吗?

我认为“xml”内容类型应该映射到“text/xml”,而不是“application/xml”。此外,要强制基于扩展的内容类型解析程序,可以尝试将“ContentNegotiatingViewResolver”的“favorPathExtension”属性设置为true(尽管默认情况下它应该为true!)

编辑:我现在在这个GIT位置添加了一个工作示例-
git://github.com/bijukunjummen/mvc-samples.git
,如果使用mvn tomcat:run调出端点,json在
http://localhost:8080/mvc-samples/rest/customers/teddy.json
和xml at
http://localhost:8080/mvc-samples/rest/customers/teddy.xml
。这使用的是JAXB2而不是XStream,因为我熟悉JAXB。我注意到的一件事是,当我的JAXB注释在客户类中不正确时,Spring提供的是JSON,而不是您所看到的XML(您可以通过从客户类中删除XMLRootElement注释来复制它),一旦我修复了注释,我就按预期返回了XML因此,您的XStream配置可能有问题。


编辑2:你是对的!!我没有注意到,一旦我得到xml,我就假设json现在可以工作了。我看到了这个问题,在
注释方法handleAdapter
中,
@ResponseBody
的处理有点奇怪,它完全忽略了viewsolver,而是使用注册的messageconverter完全绕过
contentnegotingviewresolver
,现在的一个解决方法是使用
@modeldattribute
注释来响应,而不是@ResponseBody,这样可以调用视图解析器。现在尝试使用
git@github.com:bijukunjummen/mvc samples.git
并查看它是否适合您。这可能是一个Spring bug,您可以尝试在Spring论坛中提出它,看看他们的建议。

我也有同样的问题。我假设您使用的是Spring3,您使用的是
。我不完全确定,但我认为这会在mvc命名空间配置的消息转换器的基础上产生一些冲突

使用oxm名称空间对我很有用:

@XmlRootElement(name="person")
class Person {
   private String firstName;
   private String lastName;
}

@Controller 
@RequestMapping("person")
class PersonController {
   @RequestMapping("list")
   public @ResponseBody Person getPerson() {
      Person p = new Person();
      p.setFirstName("hello");
      p.setLastName("world");
      return p;
   }
}
内容配置(mvc和内部视图解析器位于另一个上下文中):


本例使用JAXB,因此在类路径上需要JAXB api和JAXB impl

另外,只是一个提示,您不需要app-servlet.xml。在web.xml中,将配置设置为null,并让上下文侦听器为您加载它们:


org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/spring/mvc-context.xml,/WEB-INF/spring/content-negotiation-context.xml
应用程序
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
1.
应用程序
/

哪些接受头被发送到服务器?
确保要请求的内容类型在此列表中。

使用浏览器访问控制器将发送典型的浏览器接受标头。它将不匹配任何视图解析器,并且默认为第一个视图解析器(application/xml),或者它匹配,因为application/xml在接受列表中

我可以建议使用RestClient完全控制要发送的Accept头(如果有的话)


我不建议使用text/xml,因为默认字符集是US-ASCII而不是UTF-8。这可能会在以后的过程中产生一些古怪的编码问题。您始终可以指定编码,但appliance/xml有一个UTF-8默认编码

我找到了一个解决方案,但我不知道这是否是您的方法中的正确方法。向客户展示:

@RequestMapping(value = "/{id}", method = GET)
@ResponseBody
public Customer showCustomer(@PathVariable String id) {
    Customer c = new Customer("0001", "teddy", "bean");
    return c;
}
在这一部分中,我们使用的是spring的MVC,在控制器中我们应该返回一个视图,因此,我删除了注释
@ResponseBody
,并返回了一个带有视图名称的
字符串,因为在XML中我们添加了
contentnegotingviewresolver
,当我们有
ResponseBody
时,contentnegotiationviewresolver被忽略,因为它正在等待视图,但我们返回了对象,因此方法应该是这样的:

@RequestMapping(value = "/{id}", method = GET)

public String showCustomer(@PathVariable String id, ModelMap model) {
     Customer c = new Customer("0001", "teddy", "bean");
     model.addAttribute("customer",c);
    return "myView";
}
这对我很有用,如果您有问题,可以添加到您的
appservlet.xml

这个豆子,但我不认为你必须加上这个


/WEB-INF/views/
.jsp

Spring3.1解决了您提到的问题,在
@RequestMapping
注释上使用新的
生成
元素。这允许您控制Spring应用于对象的
HttpMessageConverter

我写了一篇关于它的博客:


您的控制器中app/customers/teddy.xml映射到哪里?抱歉