0

In my spring project , I am receiving notifications from a notify service after I get a notification how do I display the notification data in my browser. Here is my code.

@RequestMapping(value = "/notify/userregister", method = RequestMethod.POST)
    public ResponseEntity<String> registerNotification(@RequestBody @Valid RegistrationInfo registrationInfo) throws IOException {

        if(registrationInfo != null && registrationInfo.getregistrationInfo()!= null && registrationInfo.getregistrationInfo.size() > 0 ){
                for(int i=0;i<registrationInfo.getregistrationInfo.size();i++ ){
                    logger.info("registration notification data:"+registrationInfo.getregistrationInfo().get(i).getUserAccessId()+registrationInfo.getregistrationInfo().get(i).getRegistrationTime));
                }       

                return new ResponseEntity<String>(HttpStatus.OK);
        }
        else{
            return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
        }
    }

when my server is running , the registration service sending notification data. I am able to see the data in log file and acknowledging registration service with 200 code. I want to display the notification data (Ex:registrationInfo.getregistrationInfo().get(i).getUserAccessId())in my browser. currently it is showing "Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.there was an unexpected error....". How to get rid of this error message on my browser?

madhu
  • 129
  • 2
  • 6
  • 15
  • You can solve this by adding an ErrorController in your application. You can have the error controller return a view that you need – Mohit Sharma Oct 20 '16 at 21:50
  • could you provide me any sample code for reference? – madhu Oct 21 '16 at 13:47
  • I am using spring boot. any sample code would be a great help. – madhu Oct 21 '16 at 14:15
  • https://gist.github.com/jonikarppinen/662c38fb57a23de61c8b – Mohit Sharma Oct 21 '16 at 16:38
  • Thank you for the response. but the site is not reaching. In my project I have to send acknowledgement to service and display data if I have data inside if statement or display no user registered yet message. – madhu Oct 21 '16 at 16:56

1 Answers1

0

https://gist.github.com/jonikarppinen/662c38fb57a23de61c8b

package com.company.project.controllers;
import com.company.project.model.api.ErrorJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Based on the helpful answer at http://stackoverflow.com/q/25356781/56285,
 * with error details in response body added.
 * 
 * @author Joni Karppinen
 * @since 20.2.2015
 */
@RestController
public class CustomErrorController implements ErrorController {

private static final String PATH = "/error";

@Value("${debug}")
private boolean debug;

@Autowired
private ErrorAttributes errorAttributes;

@RequestMapping(value = PATH)
ErrorJson error(HttpServletRequest request, HttpServletResponse response) {
    // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. 
    // Here we just define response body.
    return new ErrorJson(response.getStatus(), getErrorAttributes(request, debug));
}

@Override
public String getErrorPath() {
    return PATH;
}

private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
}

}

Mohit Sharma
  • 187
  • 1
  • 13