2014-10-21 46 views
0

我有兩個@RequestMapping有兩個不同的媒體類型生產=「text/html」& produce =「application/json」,只有一個映射位置都指向value =「style/inbox」。爲什麼我的默認@RequestMapping自動選擇?

爲什麼當我不發送一個http Accept和我的請求一樣,我的應用程序/ json作爲默認值嗎?我如何設置?

使用Spring MVC。

@RequestMapping(params = "stillOhh", value = "security/inbox", produces = "text/html") 
@ResponseBody 
@Nonnull 
public String doingSamlHtmlFeed(
     @Nonnull final HttpServletRequest request, 
     @Nonnull final HttpServletResponse response, 
     @RequestParam(value = "id", required = true) @Nonnull final String sId, 
     @RequestParam(value = "spid", required = true) @Nonnull final String spId, 
     @RequestParam(value = "relays", required = false) @Nullable final String relayS){ 

     //SOME SECURITY STUFF !!! Saml federation and mapping 


    return federated.getContentString(); 
} 


@RequestMapping(params = "something", value = "security/inbox", produces =  
"application/json) 
public ResponseEntity<Map<String,String>> doingSamlJsonFeed(
     @Nonnull final HttpServletRequest request, 
     @Nonnull final HttpServletResponse response, 
     @RequestParam(value = "id", required = true) @Nonnull final String sId, 
     @RequestParam(value = "spid", required = true) @Nonnull final String spId, 
     @RequestParam(value = "relays", required = false) @Nullable final  
StringrelayS){ 

     //SOME SECURITY STUFF !!! Saml federation and mapping 

     return new ResponseEntity<Map<String, String>>(jsonMap, HttpStatus.OK) 

     } 
+0

請提供代碼,以便它可以reviwed – Aeseir 2014-10-21 22:25:13

+0

好一點......它的SAML聯盟,所以我做了一些僞代碼來解釋這更好的。 – whatkai 2014-10-22 12:12:13

回答

1

可以配置默認內容類型。因爲如果你使用的是Java的配置,並希望text/html是默認的內容類型例子,你會做這樣的事情:

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { 
     configurer.defaultContentType(MediaType.TEXT_HTML); 
    } 

} 

而且,這裏是包含有關內容協商的更多詳細信息的鏈接在Spring MVC(代碼上面的代碼片段來自本文)。

http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc