1

I'm new to Spring, and I've been trying to figure out the best way to send JSON to a spring REST controller. I've read through some similar issues on StackExchange, but trying the solutions doesn't seem to help, so I appear to still be missing something.

On the Spring side:

@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;
    }
}

And on the javascript side (name, password, and email variables contain strings):

    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'
    });

And the chrome output:

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 Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect.

I appreciate the help, and sorry if I missed something from a similar post.

Gyst
  • 101
  • 1
  • 5

2 Answers2

0

Try changing the scope of RegisterParams to public static. I don't have much experience with Spring JSON mapping but other frameworks don't handle private nested classes very well by default.

Are you using Jackson? If so, have you registered MappingJacksonHttpMessageConverter? See this SO for more info about using Jackson and Spring MVC together.

Community
  • 1
  • 1
Erik Gillespie
  • 3,929
  • 2
  • 31
  • 48
-1

You will need to leverage the Jackson Libraries here, register the MappingJackson2HttpMessageConverter and inject the same in your RequestMappingHandlerAdapter. This is how I am using it Use this in your spring configuration file -

<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"/>

if you are using maven, you can use following dependency for Jackson Libraries

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
  </dependency>
Hbargujar
  • 360
  • 2
  • 4
  • 14