简体   繁体   中英

Getting HTTP status 400-Bad Request REST

I have created a project and getting BAD request error

Below are the code snippet, apart from this i have more code in file which i dont think making any trouble.

@Controller
public class RESTController{

@RequestMapping(value="testing", method=Request method.GET)
Public @ResponseBody String TestingXml(HttpServletRequest request, @RequestBody Test test){
String welcome=test.getName();
return welcome;
}

}

Castor-mapping.xml
<class name="abc.def.model.Test">
<field name="name" type="string">
<bind-xml name="name"/>
</field>
</class>

This above code is inside mapping tag

And have written the Test.java with single field as name and getter setter.

Can any one please help Ignore the typo as writting all this from mobile, getting no error in eclipse, while creating war and deploying it over tomcat getting the bad request.

GET HTTP method doesn't support passing a request body.

You need either to use @RequestParam with primitive field (from your example it is name ), or change request method to POST/PATCH/PUT/DELETE .

Based on your example, usage of @RequestParam :

@Controller
public class RESTController{

    @RequestMapping(value="testing", method=RequestMethod.GET)
    public @ResponseBody String TestingXml(HttpServletRequest request, @RequestParam String welcome){
        return welcome;
    }

}

You can pass it like this: http://localhost:8080/api?welcome=Hi

You can read about its usage here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM