Supporting multiple content types in a Spring-MVC controller

前端 未结 2 1460
栀梦
栀梦 2020-12-16 03:53

A Rails controller makes it very easy to support multiple content types.

respond_to do |format|
  format.js { render :json => @obj }
  format.xml
  format         


        
相关标签:
2条回答
  • 2020-12-16 04:29

    Here goes the working example controller, that renders JSON and HTML both based on request Header "Content-Type".

    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.util.MimeTypeUtils;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class PersonService {
        @RequestMapping(value = "/persons/{userId}", method = RequestMethod.GET)
        public ResponseEntity<?> getPersonByName(@RequestHeader("Content-Type") String contentMediaType,
                @PathVariable("userId") String userId,@RequestParam("anyParam") boolean isAscending) throws IOException {
    
            Person person = getPersonById(userId);
            if (isJSON(contentMediaType)) {
                return new ResponseEntity<Person>(person, HttpStatus.OK);
            }
    
            return new ResponseEntity("Your HTML Goes Here", HttpStatus.OK);
            //Note: Above you could use any HTML builder framework, like HandleBar/Moustache/JSP/Plain HTML Template etc.
        }
    
    
        private static final boolean isJSON(String contentMediaType) {
            if ("application/json".equalsIgnoreCase(contentMediaType)) {
                return true;
            }
    
            return false;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-16 04:39

    In Spring 3, you want to use the org.springframework.web.servlet.view.ContentNegotiatingViewResolver.

    It takes a list of media type and ViewResolvers. From the Spring docs:

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
      <property name="mediaTypes">
        <map>
          <entry key="atom" value="application/atom+xml"/>
          <entry key="html" value="text/html"/>
          <entry key="json" value="application/json"/>
        </map>
      </property>
      <property name="viewResolvers">
        <list>
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
          </bean>
        </list>
      </property>
      <property name="defaultViews">
        <list>
          <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
      </property>
    </bean>
    <bean id="content" class="com.springsource.samples.rest.SampleContentAtomView"/>
    

    The Controller:

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class BlogsController {
    
        @RequestMapping("/blogs")
        public String index(ModelMap model) {
            model.addAttribute("blog", new Blog("foobar"));
            return "blogs/index";
        }    
    }
    

    You'll also need to include the Jackson JSON jars.

    0 讨论(0)
提交回复
热议问题