找到你要的答案

Q:Return multiple (xml and json) format from RestController in java [duplicate]

Q:返回多个(XML和JSON)从java [复制] restcontroller格式

This question already has an answer here:

I have the following question for you. Currently I am having a RestController that allows calls like this:

@RequestMapping(value = "/notifications", method = RequestMethod.POST)
public void postNotification(@RequestBody final Notification notification) {
       //DoStuffWithTheNotification
}

And here is the notification class:

@JsonPropertyOrder({"body", "sound", "other})
public class Notification {
        @JsonProperty
        String body;
        @JsonProperty
        String sound;
        @JsonProperty
        String other;

        // Bunch of getters,setters and what not
}

Now what I want to do is the following : I want to make a POST-request to this URL, but instead of providing a json-file in the body (as my code would suggest), I would like to call it with an XML-file in the body. Is there an easy way to do this, without making an entirely new Notification object?

这个问题在这里已经有了答案:

我有以下问题要问你。目前我有一个restcontroller允许这样的电话:

@RequestMapping(value = "/notifications", method = RequestMethod.POST)
public void postNotification(@RequestBody final Notification notification) {
       //DoStuffWithTheNotification
}

这里是通知类:

@JsonPropertyOrder({"body", "sound", "other})
public class Notification {
        @JsonProperty
        String body;
        @JsonProperty
        String sound;
        @JsonProperty
        String other;

        // Bunch of getters,setters and what not
}

现在我要做的是:我想做一个POST请求该URL,而是体内提供JSON文件(如我的代码的建议),我想叫它与身体中的XML文件。有没有一个简单的方法做到这一点,而不做一个全新的通知对象?

answer1: 回答1:

take a look at JAXB- an architecture for xml binding. Here is good article: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/

take a look at JAXB- an architecture for xml binding. Here is good article: http://www.mkyong.com/spring-mvc/spring-3-mvc-and-xml-example/

answer2: 回答2:

If you are using SpringMVC then you can use content negotiator to do this.Below is an small configuration

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  /**
    *  Total customization - see below for explanation.
    */
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false).
            favorParameter(true).
            parameterName("mediaType").
            ignoreAcceptHeader(true).
            useJaf(false).
            defaultContentType(MediaType.APPLICATION_JSON).
            mediaType("xml", MediaType.APPLICATION_XML).
            mediaType("json", MediaType.APPLICATION_JSON);
  }
}

XMl configuration

<bean id="contentNegotiationManager"
             class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="parameterName" value="mediaType" />
    <property name="ignoreAcceptHeader" value="true"/>
    <property name="useJaf" value="false"/>
    <property name="defaultContentType" value="application/json" />

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
       </map>
    </property>
</bean>

For futher reference refer here or simply search Spring mvc content negotiation

如果你正在使用SpringMVC然后可以使用内容协商这样做。下面是一个小的配置

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  /**
    *  Total customization - see below for explanation.
    */
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false).
            favorParameter(true).
            parameterName("mediaType").
            ignoreAcceptHeader(true).
            useJaf(false).
            defaultContentType(MediaType.APPLICATION_JSON).
            mediaType("xml", MediaType.APPLICATION_XML).
            mediaType("json", MediaType.APPLICATION_JSON);
  }
}

XML配置

<bean id="contentNegotiationManager"
             class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="parameterName" value="mediaType" />
    <property name="ignoreAcceptHeader" value="true"/>
    <property name="useJaf" value="false"/>
    <property name="defaultContentType" value="application/json" />

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
       </map>
    </property>
</bean>

为进一步参考参考这里或简单地搜索Spring MVC的内容协商

java  json  xml  rest  spring-mvc