2017-07-26 86 views
0

我试着去击中门柱控制器:休息控制器给予404

@Controller 
public class CustomerController 
{ 

    private static Logger LOG = Logger.getLogger(CustomerController.class); 

    @Resource(name = "lookupAddressClient") 
    private LookupAddressClient lookupAddressClient; 

    @Autowired 
    private HssCustomerFacade customerFacade; 

    private final RequestMappingHandlerMapping handlerMapping; 

    @Autowired 
    public CustomerController(final RequestMappingHandlerMapping handlerMapping) 
    { 
     this.handlerMapping = handlerMapping; 
    } 

    @RequestMapping(value = "/endpointdoc", method = 
    { RequestMethod.GET, RequestMethod.POST }) 
    public void show(final Model model) 
    { 
     model.addAttribute("handlerMethods", this.handlerMapping.getHandlerMethods()); 
    } 

    @Resource(name = "hssB2BCommerceUserFacade") 
    protected HssB2BCommerceUserFacade hssB2BCommerceUserFacade; 
    private static final String ERROR_MSG = "##### Error in HssB2BCommerceUserFacade.getUserdetailsForHSSTraining()-->"; 
    private static final String ERROR_MSG_END = "####"; 


    /* TODO: Need to move this controller to hsscommercewebservice once fix the hsscommerce webservice url issue fixed */ 
    @RequestMapping(value = "/webservice/userDetails", method = 
    { RequestMethod.POST }, headers = "Accept=*/*") 
    public @ResponseBody HSSUserData getUserDetails(@RequestBody final UserCredentials userCredentials, 
      final HttpServletResponse response) 
    { 
.... 
} 

凡getUserDetails()是方法。

的UserCredentials:

public class UserCredentials { 

    private String userId; 
    private String password; 
    /** 
    * @return the userId 
    */ 
    public String getUserId() { 
     return userId; 
    } 
    /** 
    * @param userId the userId to set 
    */ 
    public void setUserId(String userId) { 
     this.userId = userId; 
    } 
    /** 
    * @return the password 
    */ 
    public String getPassword() { 
     return password; 
    } 
    /** 
    * @param password the password to set 
    */ 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    /* (non-Javadoc) 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     return "UserCredentials [userId=" + userId + ", password=" + password 
       + "]"; 
    } 

Im using PostMan client: 

我打的网址:https://localhost:8011/webservice/userDetails

我的头:接受=应用程序/ JSON 内容类型=应用程序/ JSON

我的JSON身在请求:

{ 
    "userCredentials": { 
    "userId": "asdnasd", 
    "password": "nasdkask" 
    } 
} 

我得到40 4,并且不管我传递什么请求,该请求映射都不会触发。控制器正在与其他映射“/ endpointdoc”一起工作。 控制台中没有错误。

请指教。

+0

有没有理由你有'header =“Accept = */*”'? 怎么样: '@PostMapping(“/ webservice/userDetails”)' – Ben

+0

我很困惑什么应该是我的接受价值。所以我将它设置为*/*并在Postman中将值设置为应用程序/ json –

+0

我相信这就是为什么你会得到404,RequestMapping使用'header'字段来帮助确定应该调用哪个映射,它不匹配字符串'*/*'。如果你想具体声明它匹配应用程序JSON,使用: '@PostMapping(value =“/ webservice/userDetails”,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)' – Ben

回答

0

您的请求映射具有headers = "Accept=*/*"告诉春天,如果请求头中包含"Accept=*/*"

如果你希望只接受有Content-TypeAccept标题定义为application/json请求它应该只匹配到控制器方法请求你应使用RequestMappingproducesconsumes属性。

@RequestMapping(path = "/webservice/userDetails", 
method = RequestMethod.POST, // only match to POST requests 
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, // match Content-Type application/json 
produces = MediaType.APPLICATION_JSON_UTF8_VALUE) // match Accept application/json 
+0

我在下面的MediaType包中找不到APPLICATION_JSON_UTF8_VALUE,如下: 'java.awt.PageAttributes中 org.apache.tomcat.util.http.parser org.springframework.http //这是它应该是 com.google.common.net' –

+0

有趣的,它应该在'org.springframework.http'中,但如果没有,你总是可以使用字符串“application/json” – Ben

0

我已经找到了解决办法家伙:

我不得不设置CSRF处理程序属性,以允许该URL。或者,U可以将CSRF设置为此POJO :)

干杯!