Serialize POJO to application/x-www-form-urlencoded representation of the object - spring

I am using Spring/SpringMVC and I want to serialize a POJO to application/x-www-form-urlencoded representation - the very same representation that a Spring #Controller would be able to bind to a POJO if submitted via a POST request for example.
Anyone has an idea in which Spring Components should I be looking?

Have a look at this HTTP message converter implementation. Register that in Spring MVC configuration on the server-side or a RestTemplate on the client side.

Related

How to validate header parameters while calling REST services in spring mvc?

I want to validate header parameters that I am going to send while calling REST of spring MVC. The validating class should return JSon object depending on result of validation. how can I do that?

Spring MVC interceptor

I need inputs related to Spring MVC, I have a URL to which a client will send a post request with an xml as the pay load. I plan to have a controller method which maps to the requested url, I want that xml to be validated/converted to an object using jaxb before controller method is executed. And also, the controller method should have only the object as the parameter to its methods and no httprequest etc.
So, how do I achieve this? Will interceptor be helpful? If yes, how will it be done?
I plan to use Spring 3.
Simply use #RequestBody in conjunction with #Valid on a method argument and that is all you need.
public void myRequestHandlingMethod(#Valid #RequestBody YourJaxbObject jaxbObject) { … }
I strongly suggest you take a look at the Spring reference guide

Spring get request data in Dao or service layer

I m sing spring data MongoDb. in controller #requestBody User user.
So There is no data in request now. After take it from the request body.
Is there any Way in spring to get the request body data after take it from the request Object in dao layer.. Please help me out. Thx in advance/..
Spring doesn't magically store it anywhere, so you have to read it yourself in the controller layer and pass it around as a regular parameter or store somewhere yourself.

How to send HTTP post request using spring

What configuration do i need to send HTTP post request through spring. I am using java application , it's not a web project. Can i use spring to send HTTP post request? I google it but most almost all examples are using spring MVC. Can i use just spring to send HTTP post request ?
I found this bean on net but I don't know what to do after it. I am using spring3.2 and this post i think is of year 2008...
<bean id="httpClient" class="org.springbyexample.httpclient.HttpClientTemplate">
<property name="defaultUri">
<value><![CDATA[http://localhost:8093/test]]></value>
</property>
</bean>
Any suggestions?
If you are using Spring 3.0+, it will be better to use RestTemplate for sending HTTP requests. Once you wire the RestTemplate (API), you can use different methods in it to send different types of HTTP requests.
You don't need Spring just to issue a HTTP post, see this post: Using java.net.URLConnection to fire and handle HTTP requests
And yes you can use Spring in a non-web application / command line application. Just create an instance of ApplicationContext (eg: ClassPathApplicationContext with path to your beans xml configuration injected)

Managing custom Acccept header in Spring MVC

I have a RESTful web service developed using Spring MVC and without any configuration I can return objects from my #ResponseBody annotated controller methods that get serialized to JSON. This works as soon as the Accept header in the request is not set or is application/json.
As I'm getting inspired by the GitHub API specification, I wanted to implement custom mime type for my API as GitHub does, for example: application/vnd.myservice+json. But then I need to tell Spring MVC that my controllers can provide this mime type and that it should be serialized by Json (i.e org.springframework.web.servlet.view.json.MappingJacksonJsonView class).
Any idea how to do it?
You can probably do exactly what is being done with org.springframework.http.converter.json.MappingJacksonHttpMessageConverter. Since it is not a final class, you can derive your converter from this one this way:
class MyCustomVndConverter extends MappingJacksonHttpMessageConverter{
public MyCustomVndConverter (){
super(MediaType.valueOf("application/vnd.myservice+json"));
}
}
then register your converter this way:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="MyCustomVndConverter "/>
</mvc:message-converters>
</mvc:annotation-driven>
It should just work with these changes

Resources