休息控制器给出404

时间:2017-07-26 21:02:49

标签: json spring web-services controller http-status-code-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

我的标题:Accept = application / json Content-type = application / json

请求中的我的JSON正文:

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

我得到404,无论我通过什么请求,这个请求映射都不会触发。控制器正在激活另一个映射“/ endpointdoc”。 控制台中没有错误。

请指教。

2 个答案:

答案 0 :(得分:0)

您的请求映射有headers = "Accept=*/*",告诉spring如果请求标头包含"Accept=*/*"

,它应该只匹配对该控制器方法的请求

如果您希望仅接受将Content-TypeAccept标头定义为application/json的请求,则应使用producesconsumes属性{ {1}}。

RequestMapping

答案 1 :(得分:0)

我找到了解决方案的人:

我必须为CSRF处理程序设置一个属性以允许此URL。或者,U可以为此POJO设置CSRF :)

干杯!