【问题标题】:JSON to Spring - The request sent by the client was syntactically incorrectJSON 到 Spring - 客户端发送的请求在语法上不正确
【发布时间】:2014-06-20 20:01:54
【问题描述】:

我是 Spring 新手,我一直在尝试找出将 JSON 发送到 Spring REST 控制器的最佳方式。我在 StackExchange 上阅读了一些类似的问题,但尝试解决方案似乎没有帮助,所以我似乎仍然遗漏了一些东西。

在春天方面:

@Controller
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;


    @RequestMapping(value="/register",method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody UserWrapper registerUser(@RequestBody RegisterParams params) {

        logger.info("REGISTER POST!");
        User newUser = userService.registerUser(params.getEmail(),params.getPassword(),params.getName());

        return new UserWrapper(newUser);
    }
private class RegisterParams implements Serializable {
    private String email;
    private String password;
    private String name;

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

在 javascript 方面(名称、密码和电子邮件变量包含字符串):

    jQuery.ajax({
        type: "POST",
        url: '/gameserver/user/register',
        data: JSON.stringify({name:name,password:password,email:email}),
        success: onSuccess,
        error:onError
        ,contentType:'application/json'
        ,dataType:'json'
    });

还有 chrome 输出:

Request URL:http://192.168.56.101:8080/gameserver/user/register
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:71
Content-Type:application/json
Host:192.168.56.101:8080
Origin:http://192.168.56.101:8080
Referer:http://192.168.56.101:8080/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{name:nickname, password:somepass, email:something@email.com}
Response Headersview source
Connection:close
Content-Language:en
Content-Length:986
Content-Type:text/html;charset=utf-8
Date:Fri, 20 Jun 2014 11:18:17 GMT
Server:Apache-Coyote/1.1

HTTP 状态 400 -

输入状态报告

消息

description 客户端发送的请求在语法上是 不正确。

感谢您的帮助,如果我错过了类似帖子中的某些内容,我们深表歉意。

【问题讨论】:

  • 你是否在 spring 配置中添加了
  • @RameshKotha - 我将 STS 生成的 servlet-context.xml 用于基本的 Spring MVC。它包含 我假设它是为 mvc 注释设置的?
  • @Gyst,你能解决这个问题吗?

标签: json spring rest


【解决方案1】:

尝试将RegisterParams 的范围更改为public static。我对 Spring JSON 映射没有太多经验,但默认情况下其他框架不能很好地处理私有嵌套类。

你在用杰克逊吗?如果有,你注册MappingJacksonHttpMessageConverter了吗?有关一起使用 Jackson 和 Spring MVC 的更多信息,请参阅 this SO

【讨论】:

    【解决方案2】:

    您需要在此处利用 Jackson 库,注册 MappingJackson2HttpMessageConverter 并将其注入您的 RequestMappingHandlerAdapter。这就是我使用它的方式 在你的 spring 配置文件中使用它 -

    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonRequestConverter" />
            </beans:list>           
        </beans:property>
    </beans:bean>
    
    <beans:bean id="jsonRequestConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    

    如果您使用的是 maven,您可以使用以下 Jackson 库的依赖项

      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.3</version>
      </dependency>
    

    【讨论】:

      猜你喜欢
      • 2013-03-21
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多