I am using a simple form where a user can input some JSON.
I add that input to the body of the request.
When I retrieve the value from the body, it is not formatted/encoded as JSON.
Instead I get something like json=%7B%22vrt%22%3A%7B ...
How/where do I specify that the value in the body must be JSON in such way that my controller can parse it using GSON?
Thanks in advance.
Regards
The controller
#PostMapping(value = "/api/sendMessage")
public ModelAndView sendIoTMessage(#RequestBody String json) {
VehicleMessage vehicleMessage = new Gson().fromJson(json, VehicleMessage.class);
MessageProcessor.postVehicleMessage(vehicleMessage);
ModelAndView mav = new ModelAndView();
mav.setViewName("iot");
return mav;
}
The form
<form id="sendMessage" th:action="#{/api/sendMessage}" method="post">
<div class="form-group">
<input class="form-control" th:value="*{json}" id="json" name="json">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
By default, Spring uses Jackson convertor for APPLICATION JSON, if you want to use GSON convertors, then you need to add GSONConvertor.
I personly perfer Option 1
Different ways to add GSONConvertor:
Using JavaConfig
#Configuration #EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {
#Override
public void configureMessageConverters(List<HttpMessageConverter < ? >> converters) {
GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
converters.add(gsonHttpMessageConverter);
}
}
Using customize converters
#Configuration
public class CustomConfiguration {
#Bean
public HttpMessageConverters customConverters() {
Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
messageConverters.add(gsonHttpMessageConverter);
return new HttpMessageConverters(true, messageConverters);
}
}
Using Auto-configuration ..follow this link
Why not let Spring (de)serialize JSON for you? This functionality should work out of the box without any custom configuration.
#PostMapping(value = "/api/sendMessage", consumes="application/json")
public ModelAndView sendIoTMessage(#RequestBody VehicleMessage vehicleMessage) {
MessageProcessor.postVehicleMessage(vehicleMessage);
ModelAndView mav = new ModelAndView();
mav.setViewName("iot");
return mav;
}
Related
Actually, my project in Spring send values from the database via console, like this:
Console image, but I want to send this values via JSON like a rest API, but I don't know how to change that.
{
"depositarios": {
"correo": "correo",
"nombre": "nombre",
"numTel": "numTel",
"pApellido": "pApellido",
"SApellido": "sAellido"
}
}
this is my main class:
#SpringBootApplication
#ComponentScan("com.abner.springpostgresql.service.impl, com.abner.springpostgresql.dao.imp")
public class SpringPostgresqlApplication {
public static void main(String[] args) {
ApplicationContext context= SpringApplication.run(SpringPostgresqlApplication.class, args);
depoService depoService =context.getBean(depoService.class);
depoService.loadAllDepo();
}
}
this is my entired project source https://github.com/abnercoronado/restpostgresql
You have to create a RestController using the #RestController annotation like this:
#RestController
public class MyRestController {
#RequestMapping(value = "/personas", method = RequestMethod.GET)
public List<Persona> listaPersonas() {
// This is just a sample. Here you could bring your data form your db
List<Persona> lista = new ArrayList<Persona>();
Persona p = new Persona();
p.setNombre("angel");
p.setEdad(20);
lista.add(p);
return lista;
}
}
The value of the #RequestMapping annotation ("/personas" in this example) will be the endpoint. So when you access to the endpoint http://localhost:8080/personas (asuming that your app is running on http://localhost:8080) then you will get your data as json.
Here is an example of how to do it.
Here is another example (en espaƱol) that could help you.
You can use ObjectMapper to convert your pojo or object to JSON String and send where ever you wanted using thier API or anything.
Or you can create Rest Method and Access the API would return you the Json Value.
#RestController
public class MyRestController {
#RequestMapping(value = "/depo", method = RequestMethod.GET)
public List<?> getDepo() {
ApplicationContext context= SpringApplication.run(SpringPostgresqlApplication.class, args);
depoService depoService =context.getBean(depoService.class);
List<?> lista = depoService.loadAllDepo();
return lista;
}
Another way of doing.
#RestController
public class MyRestController {
#RequestMapping(value = "/depo", method = RequestMethod.GET)
public List<Depo> getDepo() {
ApplicationContext context= SpringApplication.run(SpringPostgresqlApplication.class, args);
depoService depoService =context.getBean(depoService.class);
List<Depo> lista = depoService.loadAllDepo();
return lista;
}
Once you start your server, you can run this by doing localhost:8080/depo. you can also return XML.
I have a spring MVC config with the following:
public class SpringConfiguration extends WebMvcConfigurerAdapter {
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
//Registering Hibernate4Module to support lazy objects
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(new Hibernate4Module());
messageConverter.setObjectMapper(mapper);
return messageConverter;
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//Here we add our custom-configured HttpMessageConverter
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}
}
The previous method used to ignore all lazy relation without adding JsonIgnore in model
The problem is I have a route to steam mp3 file as an octet response as following
#GetMapping(value = "/audio/{id}")
public ResponseEntity<byte[]> streamMp3FileToAdmin(#PathVariable Integer id) {
CorporateCampaign camp = corporateCampaignService.findById(id);
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(Utilities.getFileAsBytes(camp.getVoiceUrl()),httpHeaders,HttpStatus.OK);
}
If I remove jackson message converter the steaming works fine but when I add jackson message converter the stream doesn't work any more
I read this question Spring MVC: How to return image in #ResponseBody?
and a lot but I didn't find a solution yet
You need to add produces = MediaType.APPLICATION_OCTET_STREAM to the #GetMapping(value = "/audio/{id}") to specify produced result content type and let browser recognize it properly.
It's my Feign interface
#FeignClient(
name="mpi",
url="${mpi.url}",
configuration = FeignSimpleEncoderConfig.class
)
public interface MpiClient {
#RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> getPAReq(#QueryMap Map<String, String> queryMap
);
}
and my custom configuration
public class FeignSimpleEncoderConfig {
public static final int FIVE_SECONDS = 5000;
#Bean
public Logger.Level feignLogger() {
return Logger.Level.FULL;
}
#Bean
public Request.Options options() {
return new Request.Options(FIVE_SECONDS, FIVE_SECONDS);
}
#Bean
#Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder()
.encoder(new FormEncoder());
}
}
If I send request like this I see that my request send Content-Type: application/json;charset=UTF-8.
But if I set content type
consumes = "application/x-www-form-urlencoded"
I've this error message
feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] and content type [application/x-www-form-urlencoded]
at org.springframework.cloud.netflix.feign.support.SpringEncoder.encode(SpringEncoder.java:108) ~[spring-cloud-netflix-core-1.1.7.RELEASE.jar:1.1.7.RELEASE]
How to send POST request, I think I should make something more with Encoder.
Thanks for your help.
First of all you should change your Feign interface like this:
#FeignClient (
configuration = FeignSimpleEncoderConfig.class
)
public interface MpiClient {
#RequestMapping(method = RequestMethod.POST)
ResponseEntity<String> getPAReq(Map<String, ?> queryMap);
}
Then you should set the encoder during feign configuration:
public class FeignSimpleEncoderConfig {
#Bean
public Encoder encoder() {
return new FormEncoder();
}
}
It seems to me that Map is not valid for form body. MultiValueMap works just fine.
Feign client:
#FeignClient(name = "name", url="url", configuration = FromUrlEncodedClientConfiguration.class)
public interface PayPalFeignClient {
#RequestMapping(value = "/", method = POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
#Headers("Content-Type: application/x-www-form-urlencoded")
String foo(MultiValueMap<String, ?> formParams);
}
Config:
#Configuration
public class FromUrlEncodedClientConfiguration {
#Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
#Bean
#Primary
#Scope(SCOPE_PROTOTYPE)
Encoder feignFormEncoder() {
return new FormEncoder(new SpringEncoder(this.messageConverters));
}
}
Gradle dependencies:
compile group: 'io.github.openfeign.form', name: 'feign-form', version: '2.0.2'
compile group: 'io.github.openfeign.form', name: 'feign-form-spring', version: '2.0.5'
After that all you have to do is call it with a MultivalueMap parameter.
Specify the correct encoder for handle form encoded request
you can specify multi encoder example json/xml/formhttpurl encoded
#Bean
public Encoder feignEncoder() {
ObjectFactory<HttpMessageConverters> objectFactory = () ->
new HttpMessageConverters(new FormHttpMessageConverter());
return new SpringEncoder(objectFactory);
}
Important FormHttpMessageConverter serialize only MultiValueMap subsclass
im my case it was due to outdated version of lombok, updating to 1.18.16 resolve it
While implementing a File Uploader controller in Spring MVC I stucked with one problem. My code snap is given below.
#Controller
public class FileUploader extends AbstractBaseController implements HandlerExceptionResolver
{
#RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
#ResponseBody
public JSONObject handleFileUpload(#RequestParam("file") MultipartFile file)
{
JSONObject returnObj = new JSONObject();
if (file.isEmpty())
{
returnObj.put("success", "false");
returnObj.put("message", "File is empty");
}
else
{
try
{
//my file upload logic goes here
}
catch (Exception e)
{
returnObj.put("success", "false");
returnObj.put("message", "File not uploaded.");
}
}
return returnObj;
}
#Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object obj, Exception exception)
{
ModelAndView model = new ModelAndView();
Map map = new HashMap();
if (exception instanceof MaxUploadSizeExceededException)
{
// I want to return JSONObject from here like given below.
/**
* { "message":"File size exceeded", "success":"false" }
* */
map.put("message", "File size exceeded");
map.put("success", "false");
model.addObject(map);
}
return model;
}
}
and my spring configuration look likes
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize" value="300000"/>
</bean>
now In my controller I want to return JSONObject instead of ModelAndView in resolveException method in my controller as given in code snap because I am developing some like REST method to upload file.
any ideas?
Thanks
If you use the Spring 3.2 above, I recommend this way.
At first, declare the ControllerAdvice.
#Controller
#ControllerAdvice
public class JAttachfileApi extends BaseApi
And make the Exception Handler to response JSON Object as following.
#ExceptionHandler(MaxUploadSizeExceededException.class)
public #ResponseBody Map<String,Object> handleMaxUploadSizeExceededException(
MaxUploadSizeExceededException ex)
{
Map<String,Object> result = getResult();
JFileUploadJsonResponse errorResult = new JFileUploadJsonResponse();
errorResult.setError("Maximum upload size of "+ex.getMaxUploadSize()+" bytes exceeded.");
List<JFileUploadJsonResponse> resultData = new ArrayList<JFileUploadJsonResponse>();
resultData.add(errorResult);
result.put("files", resultData);
return result;
}
You simply can annotate the method resolveException as #ExceptionHandler() and then you can have its signature like any other controller method. So placing #ResponseBody before the return type should work.
"Much like standard controller methods annotated with a #RequestMapping annotation, the method arguments and return values of #ExceptionHandler methods can be flexible. For example, the HttpServletRequest can be accessed in Servlet environments and the PortletRequest in Portlet environments. The return type can be a String, which is interpreted as a view name, a ModelAndView object, a ResponseEntity, or you can also add the #ResponseBody to have the method return value converted with message converters and written to the response stream."
I'm trying to return multiple views within a single request, returning them all in a JSON string.
Example:
#RequestMapping(value = "my-request")
public void myRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
Map<String,Object> model1 = new Hashtable<String,Object>();
model1.put(...);
ModelAndView modelAndView1 = new ModelAndView("path/to/view1", model1);
// Render modelAndView1 in some way, in order to obtain the rendered HTML as a String
Map<String,Object> model2 = new Hashtable<String,Object>();
model2.put(...);
ModelAndView modelAndView2 = new ModelAndView("path/to/view2", model2);
// Render modelAndView2 in some way, in order to obtain the rendered HTML as a String
// Now write a JSON String to the HttpServletResponse containing both the rendered views (strings).
// (this is not part of my problem, I'm able to do it as long as I have the two strings)
}
I'm using Spring MVC with Tiles 2.
Can anyone help me?
Update 1 - View names are resolved using a ViewResolver:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/tiles/" />
<property name="suffix" value=".jsp" />
</bean>
Update 2 - I've created a github repository containing a very small example reproducing the problem.
UPDATE: There are some approaches for solving your issue. I'd try this one, but maybe there are cleaner ways.
#Component
public class JsonMultipleViewFactory {
#Autowired private List<ViewResolver> viewResolverList;
public View getView(List<ModelAndView> mavList) {
for (ModelAndView mav : mavList) {
if (mav.getView()==null) {
mav.setView(resolve(mav.getViewName()));
}
}
return new JsonMultipleView(mavList);
}
private View resolve(String viewName) {
for (ViewResolver vr : viewResolverList) {
View view = vr.resolve(viewName, LocaleContextHolder.getLocale());
if (view!=null) {
return view;
}
}
return null;
}
}
public class JsonMultipleView implements View {
private final List<ModelAndView> mavList;
public JsonMultipleView(List<ModelAndView> mavList) {
this.mavList = mavList;
}
public String getContentType() {
return "application/json";
}
public void render(Map<String,?> model, HttpServletRequest request, HttpServletResponse response) {
Json json = new Json(); // You can use several Json libraries here
for (ModelAndView mav : mavList) {
MockHttpServletResponse mockResponse = new MockHttpServletResponse();
mav.getView().render(mav.getModel(), request, mockResponse);
json.add(mav.getViewName(), mockResponse.getContentAsString());
}
json.write(response.getOutputStream());
response.getOutputStream().close();
}
}
And can be used like this:
#Autowired private JsonMultipleViewFactory jsonMultipleViewFactory;
#RequestMapping(value = "my-request")
public View myRequest() {
...
List<ModelAndView> mavList = new ArrayList<ModelAndView>();
mavList.add(modelAndView1);
mavList.add(modelAndView2);
return jsonMultipleViewFactory.getView(mavList);
}