Jersi 1.19, veb-xizmatiga qatorni o'tgan 400 xatolikni qaytaradi

Jersi oddiy xizmatida quyidagi kod bor:

@Path("/form")
@Consumes(MediaType.APPLICATION_JSON)
public class FormValueService {
    private static final Logger L = LoggerFactory.getLogger(FormValueService.class);

    public FormValueService() {
        L.info("Creating FVS");
    }

    @POST
    @Path("/values")
    @Produces({MediaType.TEXT_PLAIN})
    public String saveFormData(FormValue[] formValues) {
        String formId = UUID.randomUUID().toString();
        L.debug("Saving form {}", formId);
        for (FormValue fv : formValues) {
            L.debug("  Form value is {}", fv);
        }
        L.debug("Finished saving form {}", formId);
        return String.valueOf(Boolean.TRUE);

    }

    @POST
    @Path("/value")
    @Produces({MediaType.TEXT_PLAIN})
    public String saveFormData(FormValue formValue) throws IOException {
        java.util.logging.LogManager.getLogManager().readConfiguration(this.getClass().getClassLoader().getResourceAsStream("logging.properties"));
        String formId = UUID.randomUUID().toString();
        L.debug("Saving form {}", formId);
        L.debug("  Form value is {}", formValue);
        L.debug("Finished saving form {}", formId);
        return String.valueOf(Boolean.TRUE);
    }
}

Ikkinchi uslub ajoyib ishlaydi:

curl -i -X-POST -d "{" fieldName ":" asdf "," fieldValue ":" asdf "," fieldType ":" CHAR "}" -H "Qabul qilaman: matn/tekis" -H " Content-Type: application/json "localhost: 8080/ManageContact/dam olish/shakl/qiymat

Bu erda hech qanday muammo yo'q va men to'g'ri server xabarlarini olaman.

Birinchi usul har doim 400 xatoni qaytaradi:

curl -i -X-POST -d "[{" fieldName ":" asdf "," fieldValue ":" asdf "," fieldType ":" CHAR "}] - H" Qabul qil: matn/tekis " H "Content-Type: application/json" localhost: 8080/ManageContact/dam olish/form/qiymatlari

Agar bu masalalar bo'lsa, bu Jettyda ishlaydi

EDIT Bu erda dastur konfiguratsiyasi:

@ApplicationPath("rest")
public class  ContactApplication extends Application{
    Logger logger = LoggerFactory.getLogger(ContactApplication.class);

    @Override
    public Set getSingletons() {
        logger.debug("Entering ContactApplication.getSingletons()");
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyJPAJAXRS");
        ContactService contactService = new ContactService(entityManagerFactory);
        FormValueService formValueService = new FormValueService();

        Set singletons = new HashSet(); singletons.add(contactService); singletons.add(formValueService); logger.debug("Exiting ContactApplication.getSingletons()"); return singletons; } } 

Bundan tashqari, log4j xususiyatlari:

log4j.rootLogger=DEBUG, A1, LogFileAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

Logging.properties

com.sun.jersey.level=ALL
com.sun.jersey.handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleFormatter.formatter=java.util.logging.SimpleFormatter

Men buni mvn jetty: run bilan boshlayman:


                org.eclipse.jetty
                jetty-maven-plugin
                ${jetty.plugin.version}
                
                    
                        
                            java.util.logging.config.file
                            logging.properties
                        
                    
                    10
                    
                        /ManageContact
                    
                    
                        target/yyyy_mm_dd.request.log
                        90
                        true
                        true
                        GMT
                    
                
            
0
Yo'q, men hech narsani bekor qilmaganman. CURLdan 400 ta javob olaman va serverda hech narsa yo'q. Muvaffaqiyatli so'rovlar uchun, kundalik xabarlarni server konsoliga olaman. Ilovani o'rnatish uchun tahrirni ko'rib chiqing, bir istisnoni yutib yuboradigan hech narsa ko'rmayapman va barcha kunlik xabarlarim OK ko'rinadi.
qo'shib qo'ydi muallif mikeb, manba
Yo'q, men hech narsani bekor qilmaganman. CURLdan 400 ta javob olaman va serverda hech narsa yo'q. Muvaffaqiyatli so'rovlar uchun, kundalik xabarlarni server konsoliga olaman. Ilovani o'rnatish uchun tahrirni ko'rib chiqing, bir istisnoni yutib yuboradigan hech narsa ko'rmayapman va barcha kunlik xabarlarim OK ko'rinadi.
qo'shib qo'ydi muallif mikeb, manba
Yo'q, men hech narsani bekor qilmaganman. CURLdan 400 ta javob olaman va serverda hech narsa yo'q. Muvaffaqiyatli so'rovlar uchun, kundalik xabarlarni server konsoliga olaman. Ilovani o'rnatish uchun tahrirni ko'rib chiqing, bir istisnoni yutib yuboradigan hech narsa ko'rmayapman va barcha kunlik xabarlarim OK ko'rinadi.
qo'shib qo'ydi muallif mikeb, manba
Xatolikka javoban ma'lumotlar yoki serverdagi istalgan ma'lumotlar bormi? Bu odatda Jekson xatosi bo'lib, sukut bo'yicha siz javob sifatida istisno xato xabarini olishingiz kerak. Agar ushbu xatti-harakatni bekor qilsangiz, xatoni aniqlash uchun uni qaytarib oling
qo'shib qo'ydi muallif Paul Samsotha, manba
Xatolikka javoban ma'lumotlar yoki serverdagi istalgan ma'lumotlar bormi? Bu odatda Jekson xatosi bo'lib, sukut bo'yicha siz javob sifatida istisno xato xabarini olishingiz kerak. Agar ushbu xatti-harakatni bekor qilsangiz, xatoni aniqlash uchun uni qaytarib oling
qo'shib qo'ydi muallif Paul Samsotha, manba

6 javoblar

Shunday qilib, agar kimdir buni topsa, javob topdim. Muammo web.xml ilovasida servlet init bo'ldi. Formatda POJO Mapping xususiyatini yoqish uchun init-param ni kiritish kerak edi:

  
      com.sun.jersey.api.json.POJOMappingFeature
      true
  

Init servletini yaratish shunday bo'ladi:

  Archetype Created Web Application
  
        Jersey Web Application com.sun.jersey.spi.container.servlet.ServletContainer  javax.ws.rs.Application com.webspherenotes.rest.ContactApplication   com.sun.jersey.api.json.POJOMappingFeature true  1 
    
        Jersey Web Application
        /rest/*
    
0
qo'shib qo'ydi

Shunday qilib, agar kimdir buni topsa, javob topdim. Muammo web.xml ilovasida servlet init bo'ldi. Formatda POJO Mapping xususiyatini yoqish uchun init-param ni kiritish kerak edi:

  
      com.sun.jersey.api.json.POJOMappingFeature
      true
  

Init servletini yaratish shunday bo'ladi:

  Archetype Created Web Application
  
        Jersey Web Application com.sun.jersey.spi.container.servlet.ServletContainer  javax.ws.rs.Application com.webspherenotes.rest.ContactApplication   com.sun.jersey.api.json.POJOMappingFeature true  1 
    
        Jersey Web Application
        /rest/*
    
0
qo'shib qo'ydi

Shunday qilib, agar kimdir buni topsa, javob topdim. Muammo web.xml ilovasida servlet init bo'ldi. Formatda POJO Mapping xususiyatini yoqish uchun init-param ni kiritish kerak edi:

  
      com.sun.jersey.api.json.POJOMappingFeature
      true
  

Init servletini yaratish shunday bo'ladi:

  Archetype Created Web Application
  
        Jersey Web Application com.sun.jersey.spi.container.servlet.ServletContainer  javax.ws.rs.Application com.webspherenotes.rest.ContactApplication   com.sun.jersey.api.json.POJOMappingFeature true  1 
    
        Jersey Web Application
        /rest/*
    
0
qo'shib qo'ydi

Iltimos, ma'lumotlarni quyida keltirilgan formatga o'tkazib ko'ring.

{"formValues": [{"fieldName": "asdf", "fieldValue": "asdf", "fieldType": "CHAR"}]}

0
qo'shib qo'ydi
Yo'q, yo'q, lekin javob uchun rahmat. Agar sizda List <formValue> bo'lgan a'zoga ega bo'lgan sarmoyador ob'ektga ega bo'lsangiz, buni amalga oshirishning usulidir, lekin men bir qatordan foydalanishim kerak.
qo'shib qo'ydi muallif mikeb, manba
Yo'q, men aytganimdek, bu usul faqat FormValue [] ni oladi, bu faqat JSON array bo'lishi kerak, siz taklif qilgan kabi JSON obyekti emas.
qo'shib qo'ydi muallif mikeb, manba
formvalu - bu sizning custome tipi (klass), shuning uchun bizga nom berishingiz kerak
qo'shib qo'ydi muallif arjun, manba

Iltimos, ma'lumotlarni quyida keltirilgan formatga o'tkazib ko'ring.

{"formValues": [{"fieldName": "asdf", "fieldValue": "asdf", "fieldType": "CHAR"}]}

0
qo'shib qo'ydi
Yo'q, yo'q, lekin javob uchun rahmat. Agar sizda List <formValue> bo'lgan a'zoga ega bo'lgan sarmoyador ob'ektga ega bo'lsangiz, buni amalga oshirishning usulidir, lekin men bir qatordan foydalanishim kerak.
qo'shib qo'ydi muallif mikeb, manba
Yo'q, men aytganimdek, bu usul faqat FormValue [] ni oladi, bu faqat JSON array bo'lishi kerak, siz taklif qilgan kabi JSON obyekti emas.
qo'shib qo'ydi muallif mikeb, manba
formvalu - bu sizning custome tipi (klass), shuning uchun bizga nom berishingiz kerak
qo'shib qo'ydi muallif arjun, manba

Iltimos, ma'lumotlarni quyida keltirilgan formatga o'tkazib ko'ring.

{"formValues": [{"fieldName": "asdf", "fieldValue": "asdf", "fieldType": "CHAR"}]}

0
qo'shib qo'ydi
Yo'q, yo'q, lekin javob uchun rahmat. Agar sizda List <formValue> bo'lgan a'zoga ega bo'lgan sarmoyador ob'ektga ega bo'lsangiz, buni amalga oshirishning usulidir, lekin men bir qatordan foydalanishim kerak.
qo'shib qo'ydi muallif mikeb, manba
Yo'q, men aytganimdek, bu usul faqat FormValue [] ni oladi, bu faqat JSON array bo'lishi kerak, siz taklif qilgan kabi JSON obyekti emas.
qo'shib qo'ydi muallif mikeb, manba
formvalu - bu sizning custome tipi (klass), shuning uchun bizga nom berishingiz kerak
qo'shib qo'ydi muallif arjun, manba