【问题标题】:Rest controller giving 404休息控制器给出 404
【发布时间】:2018-01-02 09:24:21
【问题描述】:

我试图击中后控制器:

@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() 是方法。

用户凭据:

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 内容类型 = 应用程序/json

请求中的我的 JSON 正文:

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

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

请指教。

【问题讨论】:

  • 你有headers = "Accept=*/*"有什么原因吗?怎么样:@PostMapping("/webservice/userDetails")
  • 我很困惑我的 Accept 值应该是什么。所以我将其设置为 / 并将值设置为 Postman 中的 application/json
  • 我相信这就是你得到 404 的原因,RequestMapping 使用 header 字段来帮助确定应该调用哪个映射,它与字符串 */* 不匹配。如果您想明确说明它与应用程序 JSON 匹配,请使用:@PostMapping(value="/webservice/userDetails", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)

标签: json spring web-services controller http-status-code-404


【解决方案1】:

你的请求映射有headers = "Accept=*/*",它告诉spring如果请求头包含"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

【讨论】:

  • 我在以下任何一个 MediaType 包中都找不到 APPLICATION_JSON_UTF8_VALUE:java.awt.PageAttributes org.apache.tomcat.util.http.parser org.springframework.http // which is where it should have been com.google.common.net
  • 有趣,它应该在org.springframework.http,但如果没有,你总是可以使用字符串“application/json”
【解决方案2】:

我已经找到解决方案了:

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

干杯!

【讨论】:

    猜你喜欢
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多